IELM 511 Information Systems Design Lab 3: Fundamental PHP by kjsslv


IELM 511 Information Systems Design Lab 3: Fundamental PHP ...

More Info
									                         IELM 511 Information Systems Design
                          Lab 3: Fundamental PHP functions: I

This lab will use a few exercises to build up your experience with PHP scripts. The main
structure will remain the same: we will get inputs via forms submitted by a web client, and
the CGI program (in php) will do something with the data sent from the client, and return
HTML output back to the client. We will focus on some basic language skills for PHP.

Objectives of this lab
To learn basics of CGI programming using PHP, including
(a) Basic data structures in PHP: variables, strings and arrays
(b) Program flow control: if.. else; while..; for..; foreach..; functions
(c) Common operators and string manipulation: +, -, etc.; explode, echo, strlen, trim,…;
(d) Using some PHP functions: sendmail.

There are several websites with excellent introduction to PHP, including most functions
and several examples and tutorials. You should use your favorite one while doing this lab.
One such site is:

Lab tasks: The tasks of this lab should be done in three stages.

STAGE 1. In this stage, you will try out many simple PHP programs to learn the basics of
PHP language. Each program should be tested by running it as a CGI script.

Step 1.1. Make a simple web-form (file: run_cgi.html) with one input field and one
text-box field (use or modify the file given to you in the lab materials).

Step 1.2. Write a simple PHP program (run_cgi.php) that will respond when the form from
step 1.1, which will merely respond back to the client: “Hello, world.”

Step 1.3. Variables, if-else
PHP variables are identified by ‘$’ in front of their names. A variable can store any atomic
piece of data, most commonly, a number or a string. A string is a sequence of characters,
and is quite useful in storing text. The way to join two strings is by using a ‘dot’ operator.
Use the following code in your CGI program and observe the results.
    $a = rand( 0, 1);
    $b = rand( 0, 1);
    $lt = “ is less than ”; $gt = “ is greater than ”
    if ( $a < $b) {
      echo “I generated two random numbers, and ”. $a . $lt . $b;
    else {
      echo “I generated two random numbers, and ”. $a . $gt . $b;
What you learnt:
1. variables can store numbers or strings;
2. values of variables can be compared ( <, >, <=, >=, ==, !=)
3. rand( x, y) generates a random number uniformly distributed on [x, y]
4. flow of the program can be controlled by if ( test) {… } else { …}
5. Notice how the “.” operator is used to join strings in the last two echo statements.

Step 1.4. Arrays, iteration, and some useful string functions
PHP has two types of arrays; the simple type of array stores a sequence of variables, whose
names are indexed using ‘[ ]’, with an integer index. The other type is associative arrays, in
which the index is also specified by the program.
Use the following code in your CGI program and observe the results.
   $authors = array(“Alpher”, “Bethe”); // define an array with two elements
   $authors[2] = “Gamow”; // add a third element to the array
   for ( $i = 0; $i < count( $authors); $i++) {
     echo “Author “ . $i . “ is “ . $authors[$i];

   // Below is a useful string function that breaks a string into parts:
   echo “<p>”;
   $auth_names = “Alpher, Bethe, Gamow”;
   $each_author = explode(“,”, $auth_names); // break the string at each instance of ‘,’
   for ( $i = 0; $i < count( $ each_author); $i++) {
     echo “Author “ . $i . “ is “ . $ each_author[$i];

Use the following code in your CGI program and observe the results.
   $paper_auths = array( “P111” => “Alpher, Bethe, Gamow”, “P222” => “Gauss”);
   $paper_auths[‘P333’] = “Newton, Raphson”;
   foreach ($paper_auths as $p_no => $auths) {
      echo “Paper number “ . $p_no . “ is authored by “ . $auths . “. “;

What you learnt:
1. Arrays store groups of objects; this is useful if we need to do the same thing with each.
2. Two ways to iterate: for, foreach.
3. explode( ) function to break strings into components. What happens if you explode with
an empty string, e.g. explode( “Hello”, “”) ?

STAGE 2. In this stage, you will learn how to set up PHP so it can send emails directly
using CGI programs.
Step 1. Setting up your email server and windows environment (1)
      Go to [root disk]\xampp\apache\bin\php.ini.
      Remove “;” before each blue line listed below.
      [mail function]
      ; For Win32 only.
      SMTP = (e.g.,
      smtp_port = 25
      ; For Win32 only.
      sendmail_from =(e.g., )
      ; For Unix only. You may supply arguments as well (default: “sendmail -t -i”).
      sendmail_path = “C:\xampp\sendmail\sendmail.exe -t”
      Go to [root disk]\xampp\sendmail\sendmail.ini.
      Remove “;” before each blue line listed below.
      ; smtp port (normally 25)
      auth_username=(e.g., ust account)
      auth_password=(e.g., ust passward)
      force_sender=(e.g., )

Setp 2. Download “sendmessage.htm” from the folder “lab3_files” and save it in [root
Download “sendmail.php” and save it in [root disk]\xampp\htdocs\html\cgi-bin;
Setp 3. Comprehend the function of each file. In sendmail.php, modify the first argument
of the mail( ) function to replace nobody@no.where to your own email address.

STAGE 3. Using the functions developed earlier, write a PHP program that will do the
Suppose we have the data for a set of papers: each paper has a paper number, the names of
one or more authors, and one keyword (denoting the main field of this paper). Store this
data in two associative arrays: one array called $paper_authors, and one called
$paper_keywords. To simplify the programming, just use the last name of the authors.
We are also given a list of reviewers: for each reviewer, we know the name, and a list of
keywords, denoting the fields of expertise of this reviewer. Store this data in an associative
array, $reviewers, indexed by the name of the reviewer. e.g. there may be an entry like:
$reviewers[‘anton’] with value “cg, cad”.
It is possible that the same name appears as an author as well as a reviewer. We shall
assume that all names are unique.
Write a program to suggest which reviewer is eligible to review a given paper. The
conditions are: (1) the person who is assigned should have the paper-keyword in his
expertise-keyword list and (2) the reviewer should not have any co-authored any paper with
any of the authors of the paper.
The inputs: In this lab, we will assume that the data for the papers and reviewers are
already stored in arrays in your program (these are the two array constants in the given
skeleton program). We will send two additional inputs to the CGI program: (a) A paper
number, and (b) an email address (of the area editor, in this case, you). Modify the given
HTML file to design the proper form to send this data to your CGI.
The output: For the input paper number, your CGI program must find out the list of eligible
reviewer names, and (i) send an email to the area editor, listing the names of all possible
referees for this paper; (ii) Output the same data also to the web client.

1. HTML quick reference file.
2. PHP tutorials and function references: (from
3. PHP tutorial site:
4. Another PHP tutorial/reference:

Lab materials for IELM 511 prepared by WEI Xiangzhi, Dept of IELM, HKUST.

To top