Murdoch University Logo B211 Internet Computing

Laboratory Week 7 - Introduction to Server-side Scripting with PHP


Learning Objectives:

Before doing the tutorial, please read:

Software required:

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

  1. 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.

  2. 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:

    1. 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.

    2. 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.

    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.

    4. 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.php

      You 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

Exercises:

  1. 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.

  2. 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).
  3. 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

  4. 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