Introduction
After the first coursework which was the animation of the moving man we started looking at the server side and went into PHP. This coursework had to be done mainly with the use of PHP.
What to do?
We were asked to write a remote Web Space management system and the user is to be able to add and organise files to this area. This area is to be secured with a password. The theme of this management space was to be of anything we wanted.
Features:
(1) Your program should be written as PHP scripts . The user should be able to work with this program via a Web browser from any computer connected to the Internet.
(2) When your program starts, it should ask the user to enter a password and username, and not allow the user to access their Web space unless they know the password. Passwords should be stored in an appropriate MySQL database.
(3) Once allowed access the user should be able to:
- Make and delete directories
- Delete Files
- Upload new files
- Only be able to access their own space, which should have space limits imposed upon it.
Implementation:
First of I started by deciding what theme I want to use. Since this is a school coursework I taught of something that has to do with school. Therefore the theme I choose is having a school website which offers advanced courses to students and their students have to submit their homework and tests online. Three directories will be used; homework, tests and results. In the homework folder they can each upload their homework in that folder. Tests are uploaded in the tests folder and then they can download their test results from the tests folder.
I continued by designing the layout of this site. A simple image was created where it had different sections; courses, submit homework, register and contact us. This time in my work in HTML I introduced hotspots. Hotspots are, when you have one image divided into regions (hotspots). When this region is clicked an action can occur, like taking you to another page. This is done by first giving the image a usemap=" "; attribute, this will be like an id to show the image which map to use in case you have more than one. Then you select the shape of the hotspot and mark its coordinates and put the link to where you want the users to go when they click on it.
Next I researched how to protect the script from SQL injection. SQL injection is when someone inserts an SQL statement on your database without your permission. This usually happens when a user is asked to input their details and instead of a name they give you a MYSQ statement that you won't know about. I researched for code that will protect the work against MYSQL injection and used it in my SQL and fixed it to suit my code.
References:
http://www.tizag.com/mysqlTutorial/mysql-php-sql-injection.php
http://www.w3schools.com/PHP/php_file_upload.asp
http://php.net/manual/en/function.opendir.php
I continued by designing the layout of this site. A simple image was created where it had different sections; courses, submit homework, register and contact us. This time in my work in HTML I introduced hotspots. Hotspots are, when you have one image divided into regions (hotspots). When this region is clicked an action can occur, like taking you to another page. This is done by first giving the image a usemap=" "; attribute, this will be like an id to show the image which map to use in case you have more than one. Then you select the shape of the hotspot and mark its coordinates and put the link to where you want the users to go when they click on it.
Example: <area shape="rect" coords="55,464,140,496" href="courses.php" alt="courses" />

I added CSS to give the page more style and linked each page with the CSS style sheet page.
| Index Page |
After defining all the pages I started off by building the login. A database was created where the usernames and passwords are stored.
This database was created for the login, if one of these users login using these following username and password they will login successfully, if however they miss type there details or someone else will try and login with fake details, they won't be logged in.
After that I kicked off with the login. I created a form with two fields username and password and login and reset buttons added some CSS to give it some style. The login button will go to a page which has the validation process while the reset button will clear the details from the fields. I then started creating the page which validates the username and password. I started by connecting the database.
The above screenshot shows the code to the connection, $host, $username, $password and $db_name are variables I created to use them. It's better to declare the variable then using the real values throughout the pages, because if you decide to change them, then you would have to change the whole thing throughout the code and like this you just change the values of the variables.
Then the table from which the usernames and passwords are validated is passed. This was done using the SELECT*FROM statement.
Next if the logging is validate, the user will be directed to another page which is the space management, while if the login fails he will be shown an error message and he will have a try again link which will direct the user the the login part again.
| Logged in |
| Login Failure |
Next when the user manages to login he needs to select a directory so that one can upload the file he wants into the directory he wants to choose. To do so first I created a folder named uploads and created 3 different folders in it; homework, tests and results. Each folder will have its specific use. After that I did the php code. To list a directory one needs to use the readdir()function. This function reads the entry from the directory. First it opens the current directory by giving it the correct path. Then an array was defined to hold the files. Then a loop will loop through all the files and it will store the filename and close the directory using the closedir() function. After that a foreach loop was done and in it there is the echo which will display the select option for each file. With this function the user is able to select his folder in which he wants to upload his work.
After this, the user needs to upload the file. The user will first browse his computer to select the file that he would want to upload and then he uploads the file. Then if the file is uploaded successfully the user will be directed to another page where he will be shown the file details; file name and file type and then he will informed that his file has been uploaded successfully. If the file won't be uploaded successful the user will be shown an error message and he will be have a go back link.
In this page I encountered a lot of problems and had a lot of trouble, it was not finding the path and had some undefined indexes errors. Regarding the path I tested it by first trying to direct the path to somewhere else, I used My Documents as a testing path and this worked perfectly. Then I learnt that in PHP one has to use \\ slash instead of single slashed. After that I had to add the file that the user would have chosen by adding .$_POST['fileFolders']."\\". Another problem I found was to show the size of the file. For some reason it didn't work and couldn't find a way to solve it.
View Files
The user then has the ability to view the files that there is found in the directory. This was done by using the opendir() function. First the path was specified of which folder you want to view, then an if statement was used to open the given directory and proceed to read its contents. This works by the user clicking the view files button and if this works the user will be directed to view the files in the directory. Then to go back to the management system he uses a link to go back. In this part I also encountered a problem which also was a path problem because of the single slash. When I changed it into double slashes it worked.
Delete Files
After this, the user needs to upload the file. The user will first browse his computer to select the file that he would want to upload and then he uploads the file. Then if the file is uploaded successfully the user will be directed to another page where he will be shown the file details; file name and file type and then he will informed that his file has been uploaded successfully. If the file won't be uploaded successful the user will be shown an error message and he will be have a go back link.
In this page I encountered a lot of problems and had a lot of trouble, it was not finding the path and had some undefined indexes errors. Regarding the path I tested it by first trying to direct the path to somewhere else, I used My Documents as a testing path and this worked perfectly. Then I learnt that in PHP one has to use \\ slash instead of single slashed. After that I had to add the file that the user would have chosen by adding .$_POST['fileFolders']."\\". Another problem I found was to show the size of the file. For some reason it didn't work and couldn't find a way to solve it.
| Uploaded |
| Error in Uploading |
View Files
The user then has the ability to view the files that there is found in the directory. This was done by using the opendir() function. First the path was specified of which folder you want to view, then an if statement was used to open the given directory and proceed to read its contents. This works by the user clicking the view files button and if this works the user will be directed to view the files in the directory. Then to go back to the management system he uses a link to go back. In this part I also encountered a problem which also was a path problem because of the single slash. When I changed it into double slashes it worked.
| View Files |
Another function which was required to do was the delete function. My idea of this was making a check box near each file found in the directory and when the user would want to delete any file the user will check the box to delete the folder. I tried doing this using the unlink()function. Also since one users can create directories one users have to have the ability to delete them this is to be done using rmdir()function. This biggest problem in this was to make the check-boxes synchronize Unfortunately this function did not work as I taught it would.
Space Limit
Also we had to create space limits so that they won't exceed their space limit. This can be done using the getDirectorySize()function. My aim for this function was, letting the user uploads and when he exceeds his limit the user would be informed and won't be able to upload any more files. Also this function did not work.
Mobile
This site was also required to work from a mobile. This was done by typing in my IP address in the URL of the browser on the mobile. It did work good, it uploaded and one could view it but the problem was that it didn't fit perfectly on the screen since it is a small screen one would need to do a lot of scrolling. But this could be fixed by instead of giving the width a fixed size using pixels, percentages are used so that they size would fit on every type of screen size.
Conclusion
Improvements
As improvements it could have come a bit better, since not everything works I would start by trying and making those functions working, fixing errors for exceeding size limits, deleting files and also deleting folders. Also I have a registry form and a forgot your password function. Also these can be as an improvement. I did some research regarding these two pages though they look like they require advanced PHP and the registry form also needs some PHP.
Conclusion
Although I didn't manage to get this project working as the required requirements I am still happy by how far I've gone mostly because I have never coded PHP before. I will continue working and improving this project and blog about my improvements as it was a really interesting work and also will definitely come in handy in the future. I tried giving much detail as possible though this blog was done in a few time since bloggert had some problems and my previous post got deleted and had to do it all again!. I think PHP is a really interactive language and provides a lot of interactive parts with the users. It isn't such a hard language to code. I will keep on working with PHP as practice makes perfect.
http://www.tizag.com/mysqlTutorial/mysql-php-sql-injection.php
http://www.w3schools.com/PHP/php_file_upload.asp
http://php.net/manual/en/function.opendir.php
No comments:
Post a Comment