Laboratory Week 7 - Introduction to Server-side Scripting with PHP
Learning Objectives:
- Learn to set-up PHP scripts so that they work on on a web server.
- Learn to access PHP scripts using web browsers and using HTML forms.
- Learn to write simple PHP scripts.
- Learn to manipulate data in PHP using variables and control structures.
Before doing the tutorial, please read:
- Week 5 lectures on PHP programming.
- Review what you learnt in the week 3 exercises on HTML.
- Textbook Web Programming chapter 12.
Software required:
- This lab requires connection to the server phoenix.murdoch.edu.au via a SSH and an FTP program. Instructions on how to do so is given in lab week 3.
- This lab also requires a graphical web browser (like Netscape Navigator or Microsoft Internet Explorer), and a text editor (like Notepad).
Instructions:
Unlike JavaScript, which can be loaded from a local HTML file and executed locally by the browser, a PHP script must be served by a web server that has a PHP interpreter. In this unit, you will be using the web server on phoenix.murdoch.edu.au to serve your PHP scripts. You have already made use of phoenix's web server to serve you HTML documents in week 3's exercises by putting them into a directory called public_html. For PHP scripts, you will do the same.Trying the examples from the lectures
- Use a web browser to try out all the example PHP scripts in week 5 lecture 1. Just type the given URLs as shown in the screen dumps.
- Copy the sample code in the lecture and get them to work in your own directory on phoenix. To do so, you will need to do the following for each of the scripts:
- Cut and paste the scripts into text files on your local machine. Make sure you give the files .php extensions. Also, be careful when copying from the lectures MS-Powerpoint files to text files. Sometimes the double-quotes are not copied as ASCII text double-quotes. You can see this when you view your text file, and the opening quotes is slanted in a different direction than the closing quotes. When you see this, delete the double-quotes and type them in again.
- Use an FTP program to upload the file to the public_html directory on your account on phoenix. You should have learnt how to do this in week 3.
- To access the script (either directly using a browser, or through the ACTION field in an HTML form), use the URL similar to the screen dumps in the lectures, except instead of using "phoenix.murdoch.edu.au/~s12345678/examples/...", use your own URL.
- Most FTP programs should set the default file permission correctly. You can determine this by seeing if step (iii) worked. If you get a permission denied error message, then your FTP program may not have the permission set correctly. You must set the file permission for the file so that the web server can execute it (this is called making the file executable). You need to TELNET or SSH to phoenix as you did in week 3, and then use the chmod (ie "change mode") command. Follow the example below, but replace s12345678 with your own student number, and the file name boring.php with your own file name:
login as: s12345678
Sent username "s12345678"
s12345678@phoenix.murdoch.edu.au's password:
Sun Microsystems Inc. SunOS 5.8 Generic February 2000
phoenix:~> cd public_html
phoenix:~/public_html> chmod 750 boring.phpYou will need to do this only once for every new script you create.
[Note: For those who are familiar with UNIX files, the files needs to be set to be group readable and executable, since the web server on phoenix is running as a user in the same group as your user account. You should NOT set permission for public readable.]Get each one of the scripts you tried in (1) to work in your own directory on phoenix.
To edit PHP scripts
To make changes to a PHP script locally:
- Use a text editor like Notepad and open the script file locally.
- Once you have changed the script, save the file, and upload it to the server using FTP again. You do not need to do chmod anymore, unless the file name have changed.
Another possible way of making changes to the script is to edit the script file directly on the phoenix server's file system. This will save you from having to FTP to the server everytime you make a change. This requires you to TELNET or SSH to phoenix, and use an editor like pico or vi to edit the script file. You need to have some familiarity with the Unix command line to do this.
Exercises:
- Write a PHP script that stores your name and student ID in two variables. The script should print out the values of those variables in bold when it is accessed by a browser. Eg. It should show something like the following on the browser:
Hello Bart. Your student ID is s66666666.
- Modify one of the examples HTML forms from the lectures so that the form contains a text box for the user to enter their names, and another textbox for the user to enter a student ID. Get the form to send that information to your PHP script in (2). Change your PHP script so that it prints the values from the form instead of from the variables you created in (2).
- Modify your script in (3) so that it stores a list of months in a year in an array. Have the script print out the user's name and student ID, followed by a list of the months (one month per line). If the user typed in "Bart" and "s66666666" in the HTML form, the following output should shown up on the browser:
Hello Bart. Your student ID is s66666666.
The months of a year are:
January
February
March
April
May
June
July
August
September
October
November
December- Modify your script in (4) so that instead of using a normal array, it stores the number of days in each month (ignoring leap years) in an associative array, using the month name as key. Add a textbox to the HTML form so that the user can type in a name of a month. Have your script print out the number of days the month given by the user has. If the user typed in "Bart", "s66666666" and "January" in the HTML form, the following output should shown up on the browser:
Hello Bart. Your student ID is s66666666.
The month of January has 31 days.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, 15-Sep-2002 04:52:00 MST
Disclaimer & Copyright
Notice © 2002 Murdoch University