Laboratory Week 9 - Writing to Files and Manipulating Cookies in PHP
Learning Objectives:
- Learn to store data into a file in PHP.
- Learn to use cookies to store and retrieve data on the client side.
Before doing the tutorial, please read:
- Week 5 lectures on PHP Programming.
- Review what you learnt in the week 7 exercises.
Software required:
- This lab requires the use of a SSH and an FTP program. The software is available in the internal labs. External students may download the required software from the unit's downloads page.
- This lab also requires a graphical web browser (like Netscape Navigator or Microsoft Internet Explorer), and a text editor (like Notepad).
Exercises:
- Retrieve the script for exercise 2 in week 7's lab. Modify the script so that besides printing the results to the browser, the script also prints the user's name and ID into a file, separated by a semicolon ";".
You can check whether the correct data has been put into the file by either using the command "more filename.txt", where filename.txt is the name of your file, in a SSH session to phoenix. You may also check by using your FTP program to download the file and view it.
- Modify the script from exercise (1) above so that instead of rewriting the file, the script only appends the name and ID to the end of the file.
- Create a new HTML form that asks a user for a name. Create a script that processes the name given by the user. The script should open the file created by your script in exercise (2) and match the name against the names in the file. If the script finds the name in the file. It should display the line "You have been here before!". Otherwise, it should display "Welcome for the first time."
- Test the testcookie.php script given in page 36 of lecture week 5 lectures 2&3. A live version of the script is available at http://phoenix.murdoch.edu.au/~s12345678/examples/testcookie.php. Refresh the page a few times, and you will see what the date and time of your last access to the page was.
Create your own PHP script that expects a cookie called "username". If it detects the cookie, then it should print a welcome message with the username in it (eg. if the username is "John", it should display the message "Hello, John."). Otherwise it should display an HTML form asking the user to type in for. The action field of the form should be this same script. Regardless of whether it detects the cookie or not in the request, it should still send a cookie called "username" in the response.
Test your script by accessing the PHP script directly - you do not need a HTML form for this. Check that the first time you accessed the page, you get a form, but not subsequently. Set the expiry time of your cookie to be relatively short, so you will not have to wait a long time to retest the first access.
- Create an simple online Guest Book.
- Write a HTML page with a form containing three text-boxes named "Name", "Date" and "Comment". Write a PHP script that reads the 3 parameters in (i), and append the data into a file, separating the 3 fields with a semicolon. Test your script using your HTML page.
- Write another PHP script which reads from the file created by the script in (i), that sends the content in the file back to the browser as plain text. Add your page in (i) a text hyperlink "View past comments" that points to this new script. Test your script by using you HTML page.
- Modify your script in (ii) so that the results are send back to the browser properly formatted in HTML instead of plain text. Access the orginal page in (i) and test it.
- Create a cookie for your script in (i).
This completes all the programming exercises required for you to start on your assignment 2. Please proceed to doing the assignment. To assist you further, here is a link to an example simple search facility. Below are the source code files for them.
Form for user to type in a keyword. Please rename to .html extension.
PHP script to process the form and return a result - please rename to search.php. Here is the same script with comments on what it does.
Data file containing content to be searched.Try and get this simple example working in your own directory. You may use this example as a basis for developing some parts of assignment 2.
Remember to read the week's development on the Internet by scanning through relevant articles in the IT section of Tuesday's the Australian newspaper - use the online version or the physical copy in the University library. Get into the habit of keeping up with current developments. Test your knowledge of major developments by going through the Good News Week self-tests.
H.L. Hiew
Unit Coordinator
Document author: H.L. Hiew,
Unit Coordinator
Last Modified:
Sunday, 13-Oct-2002 22:53:00 MST
Disclaimer & Copyright
Notice © 2002 Murdoch University