Intro to PHP (refresher)

Running an Apache PHP webserver on silas

/usr/local/bin/makeserver.readme

Operation:

by Richard Kershaw

In its default behaviour it creates an enviroment for the php-oracle-silas-apache server.

It takes two variables for the port and the root of the new server. The root of the server defaults to the users home directory and appends apache to it. Any value added will have /apache appended to it.

It creates serveral files/directories

Files:
$root/apache/httpd.conf
$root/apache/stopserver
$root/apache/startserver

Directories
$root/apache/logs
$root/apache/cgi-bin
$root/apache/htdocs

All of the directories start empty. The access and error logs will be created in the logs directories once the server is running. Content should be added to the htdocs directory.

Known Bugs:

You may need to explicitly set the enviroment for LDAP

tcsh:
setenv LD_LIBRARY_PATH "/usr/local/openldap-2.0.7/lib"
sh:
set LD_LIBRARY_PATH="/usr/local/openldap-2.0.7/lib"
export LD_LIBRARY_PATH

What can you do on silas?

Example of PHP on Silas

Arrays in PHP

An array in PHP is actually an ordered map which maps values to keys. An array can be thought of in many ways. Each of the concepts below can be implemented in a PHP array, so you can choose which ever of these ideas that you understand to conceptualise an array.

Example: Numerically-indexed arrays (Vector array)

Say that we have a list of marks out of 100 in a subject
95, 93, 56, 70, 65, 98

$marks = array (95, 93, 56, 70, 65, 98);
generates a numerically-indexed array

Example: Numerically-indexed arrays (cont)

The following code also generates a numerically-indexed array, allocating the next index after the highest current index to the element.
$marks[] = 95;
$marks[] = 93;

marks[0] is 95 and marks[1] is 93.
Note that array indexes start at 0 by default.

You can skip indices by allocating a specific index to a value -
$marks[5] = 56;
$marks[] = 70;
will be allocate 70 to $marks[6].

marks[5] is 56 and marks[6] is 70.

Associative arrays

Say we have a list of marks out of 100 in a subject and we want to know who got what mark:
Adrian - 95, Matty - 93, Lance - 56, Stephen - 70, Craig - 65, Andy - 98

$marks = array ("Adrian"=>93, "Lance"=>56, "Stephen"=>70, "Craig"=>65, "Andy"=>98);

List an associative array

list() in conjunction with each() assigns a key / value pair into the variables $key and $variable. The following code prints each key / value pair into a table. Note that $value might itself be an array.

    reset($marks); // go to the beginning of the array
    echo "<table border=\"1\">"
    while (list($key, $value) = each($marks))
    {
       echo "<tr><td>$key</td><td>$value</td></tr>\n";
    }
    echo "</table><hr>";
    

List an associative array (cont)

each() actually returns a array for each array item which includes the key and value as well as the index 0 mapped to the key and the index 1 mapped to the value. Hence if you are more comfortable with numeric indexes, you can do the following:

    reset($marks);
    while ($row = each($marks))
    {
       echo "Mark for $row[0] is $row[1]<br />";
    }

Arrays Summary

PHP for Forms

PHP Predefined variables

PHP has a range of predefined variables available - for example Apache variables, environment variables and PHP-specific variables

Form example 1

This is an example of a form and a script which processes the input from the form. It prints out the values and sends an email to a set address and a copy to the sender.

<?php 
   include ("/cc/staff1/l/lzwise/apache/htdocs/includes/functions.php");  
   
   $pageTitle = "Sample form";
   
   // select error reporting level
   error_reporting  (E_ERROR | E_WARNING | E_PARSE);
   //error_reporting(0);

   require ("/cc/staff1/l/lzwise/apache/htdocs/lib/header_lzw.php"); 
  
// ******  CONTENT GOES IN HERE (it is in a table cell)
// Note that this could be a straight html file
?>

<form action="form_partb.php" method="post" name="test_form">
   <table width="100%" summary="Sample form for name, email and comment">
      <tr>
         <td>Enter first name:</td>
         <td><input type="text" name="fname" /></td>
      </tr>
      <tr>
         <td>Enter last name:</td>
         <td><input type="text" name="lname" /></td>
      </tr>
      <tr>
         <td>Enter email address:</td>
         <td><input type="text" name="email" /></td>
      </tr>
      <tr>
         <td colspan="2">Enter comment:</td>
      </tr>
      <tr>
         <td colspan="2"><textarea rows="5" cols="50" name="comment"></textarea></td>
      </tr>
      <tr>
         <td colspan="2"><input type="submit" name="submit" value="Send comment" />
                         <input type="reset" name="reset" value="Clear form" /></td>
      </tr>
   </table>
</form>

<?php
// ******  CONTENT FINISHES IN HERE (it is in a table cell)

   require ("/cc/staff1/l/lzwise/apache/htdocs/lib/footer_lzw.php"); 
?>

Form processing example 1

This script processes the form.

<?php 
   include ("/cc/staff1/l/lzwise/apache/htdocs/includes/functions.php");  
   
   $pageTitle = "Sample form";
   
   // select error reporting level
   error_reporting  (E_ERROR | E_WARNING | E_PARSE);
   //error_reporting(0);

   require ("/cc/staff1/l/lzwise/apache/htdocs/lib/header_lzw.php"); 
  
// ******  CONTENT GOES IN HERE (it is in a table cell)

      while(list($key, $val) = each($HTTP_POST_VARS))
      {
         echo "Variable $key contains <strong>$val</strong><br />";
      }
      
      // create an email message    
      
      // subject line
      $subject = "Input from my comment form";
      
      // generate email headers
      $headers = "From: ".$email."\r\n";
      $headers .= "Cc: ".$email."\r\n";
      $headers .= "Content-type: text/plain\r\n";
      $headers .= "X-mailer: PHP/ ".phpversion();
      
      // set the to address
      $to = "lisa.wise@its.monash.edu.au";
      
      // build the message
      $msg = "From $fname $lname\n\n".$comment;

      // use PHP's mail function, which uses sendmail to send mail
      mail($to, $subject, $msg, $headers);

?>

<?php
// ******  CONTENT FINISHES IN HERE (it is in a table cell)

   require ("/cc/staff1/l/lzwise/apache/htdocs/lib/footer_lzw.php"); 
?>

Form example 2

This is an example of the same functionality in a single script which displays a form and processes the input.

<?php
   // test if form has been filled in
   if (!isset($submit))
   {
      // output form here
   }
   else
   {
      while(list($key, $val) 
               = each($HTTP_POST_VARS))
      {
         echo "Variable $key contains 
             <strong>$val</strong><br />";
      }
      // create an email message    
      // subject line
      $subject = "Input from my comment form";
     
      // generate email headers
      $headers = "From: ".$email."\r\n";
      $headers .= "Cc: ".$email."\r\n";
      $headers .= "Content-type: text/plain\r\n";
      $headers .= "X-mailer: PHP/ ".phpversion();
      
      // set the to address
      $to = "lisa.wise@its.monash.edu.au";
      
      // build the message
      $msg = "From $fname $lname\n\n".$comment;

      // use PHP's mail function, 
      // which uses sendmail to send mail
      mail($to, $subject, $msg, $headers);
}
?>

Screening User Input/Output

Using custom functions

If you have some things that you do in a number of different scripts, you might consider putting them into custom functions. You could collect them into a file called functions.php and include them in all your scripts, or you could name them individually and include them only as needed.

For example, you might want to make your own mail function which includes some default values. To make it flexible, you will want to pass it information to use in different circumstances

<?php
function my_mail ($subject, $msg, 
     $address="lisa.wise@its.monash.edu.au")
{
   $headers = "From: ".$address."\r\n";
   if (!(strcmp($address, "lisa.wise@its.monash.edu.au")))
   {
      $headers .= "Cc: ".$address."\r\n";
   }
   $headers .= "Content-type: text/plain\r\n";
   $headers .= "X-mailer: PHP/ ".phpversion();
   $to = "lisa.wise@its.monash.edu.au";

   mail($to, $subject, $msg, $headers);
}
?>

To use this function, you would type my_mail("My Subject", $msg, $email);

Inside the function, $subject will be set to My Subject, $msg will be set to the value of the variable $msg in the script, and $address will be set to the value of the variable $email in the script

Upcoming sessions ???