Introduction
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.
This blog post will detail how the coursework of the webspace management system was created and explaining all of its features. The system will consist of 3 main pages: the login page, the mainpage and a registration screen.
Here is the explaination given to us on what we should do.
Coursework Task
Write a remote Web space management system suitable for handling Web site content of various types. It should allow a user to add and organise files to a personal Web area which is secured by password. Again, there will be a connected theme, to be announced in lecture time.
This program should include the following 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:
a) Make and delete directories
b) Delete files
c) Upload new files
b) Delete files
c) Upload new files
d) Only be able to access their own space, which should have space limits imposed upon it.
User data and passwords should be stored in an appropriate MySQL database or file system which the PHP script can access; you must consider security implications. There are no restrictions on the style of the user interface, although you should consider ease of use and design to be issues too.
Login Page
I started off with building on the login screen created in last week’s PHP with MySQL blog post. The main improvement on the Login Screen was that an extra button was created called Register which will be used to redirect the user to the registration screen. I also gave the login screen a bit of a space theme although I am no designer this is the best I could come up with.
Apart from this new button the login form has still basically the same functionality as before using cookies for the “remember me” function for automated logins and using sessions to remember the username when the user is redirected to the mainpage so that only his page is viewed.
Registration Screen
This page consists of three textfields in which the user will fill in the Username desired to register and the password for the user. The 3rd textfield will be used as a confirm password as most registration form are. Again the Space theme was kept throughout for consistency.
When the user has filled in all the fields the user will press the register button which will first check if all the fields are filled in, then it will check if the password and confirm password match. If they do, an SQL connect is created with the localhost server and an SQL query is sent to the server to find out if the username chosen by the user already exists. This is done by selecting all the rows from the Users table where the Username is the username inserted by the user. If the result of the query has a row it means that the username already exists.
If the Username doesn’t exist the username and password chosen are inserted in the table using the “mysql_query”. After the record is inserted the program will try to create a folder on the Server with the username as the folder name. This folder will be used as the folder loaded in the mainpage when this new user is created. If the folder is created a Session is created detailing that the registration is successful and the user is redirected to the login page.
The login page is checking for a session called Successful which is only created when a registration is successful, if its value is “Yes” an alert box is shown to let the user know that the Registration was successful. This was done in this way since if the alert box is created in the registration page, it is not shown due to the page is redirected to the login page, even if the alertbox is before the redirecting in the coding.
MainPage
When this page is loaded a check for a session named Username is done to write the Welcome + username. On the page, Also the session is used to open the folder using the “opendir” function in Php, the folder where the user will upload all his files since when this is created the name of the folder is the username chosen by the user. If the session is not found, the page is redirected back to the login.
This page consist of a file input with a button to start the event, a textbox with a button to add a folder with the name inserted in the textbox, a button to go back to the previous directory if you accessed a folder inside your personal space, a logout button to go back to the login screen, and a list of all the files and folders inside the space of the user name with a delete button near each one to be able to delete it. Here is a detailed explanation of each feature in the main page and how it was achieved using PHP.
The feature to load the files and folders of the user is achieved by using the “is_dir” feature to know if the item loaded is a folder or a file. If it’s a folder instead of just showing just showing the name of the file, the name of the folder would be a hyper link that would call a function to show all the files inside that particular folder by sending the name of the folder and adding it to the path used to get all the files from.
To know which files and folders to show when using the “opendir” function is that the directory which is opened, is the one created when the registration was done is used by going to the main folder of the webspace. Then adding the Username to the directory, since that is the one created.
Also when showing every file and folder name a delete hypertext field is created to be used to delete the file or folder. This hyper link sends the name of the file or folder to a function which check if it is actually a file or a folder first using the "is_dir()" and then if its a file, it deletes it using the unlink feature. If its a folder it first has to go into every file and unlink it one-by-one before deleting the directory since PHP would give an error if it tried to delete a folder which had any kind of content.
As explained before another function in the Webspace is the ability to Upload Files. This is achieved by using the file input and HTML; the user will select a file from his computer and then click the upload file button. The PHP code will check if the file size is larger than 2MB the file is not loaded an error is given. Also an error is given if the size of all the items in the folder used by the user plus the size of the file to be uploaded exceeds 10MB. I chose the limit to 10MB so that I did not need to upload a lot of files before the limit is reached and testing could be done properly. When the file is uploaded the page is reloaded using the HEADER function so an alertbox shows the user that the folder is created and it is now shown since the page is reloaded the new folder is now shown as well.
To Find out how much space is used, a function was created that will add every files' size to a variable in the main directory of the user, and then before uploading the file adding it to the size of the file that is going to be uploaded now. This is done because It could be that the file that will be uploaded now will exceed the maximum limit.
Another feature is to add new folders in the webspace. A Textbox along with button to add folder exists to make this function properly. This feature is achieved when pressing the “Add folder” button and the PHP script will take the value of the textbox and using the “mkdir” function in PHP a folder is created. Again the program checks if the folder already exists and if it does the folder is not created. After the folder is created the program uses the header so that the user will go back to the current page, so that an alertbox shows the user that the folder is created and it is now shown since the page is reloaded the new folder is now shown as well.
If the User accesses a folder inside his webspace, every item uploaded or folder created will be now inside that particular folder. Also if the user wishes to access the main folder again, he does not need to log out and log back in. The "Back to Previous Directory" will take the user up one level in the directory using the "dirname" function in PHP which gives you the directory name of where an item currently is so by giving it the current path, the directory which it will give us is the folder we need to go back to the previous directory. A validation is created so that the previous will not go further than the main screen of the specific user.
Also a very simple logout button is used to unset and destroy the sessions used in when logging in and the page is redirected back to the login page.
In the end this is what the main page looks like.
Conclusion
After finishing everything I made sure that the website is compatible in all browsers, since I kept the CSS to a minimum, everything was the same in every browser tried (Google chrome, Firefox 4, Internet Explorer).
Although I have spent a lot of time on this coursework I still believe that a lot of improvement on features could be done such as Downloading files that one has uploaded, creating a folder offline for the user which would sync with the webspace, or having an e-mail sent to the user whenever he registers first and a feature to send him an e-mail with the password whenever he forgets it.
Also I think that some improvements on styling could be done but since I never had any styling or designing experience I tried to keep things simple.
Some limitations I have done creating this Coursework was that every page had only 1 file that had all its PHP scripts. This proved to become quite messy when fixing errors or trying to find specific code. Next time I would make sure to structure the scripts into more files for at least every main function and link them to the main page using the "include" function.
As For my Experienced when using PHP over these last few weeks, coming from .NET Developer who never did any Web Development before was, that PHP although a very powerful scripting language still leaves us some things to be desired such as a good Debugging tool and maybe its strictness over Case-sensitivity can be a little tedious.
No comments:
Post a Comment