Lab Notes Eight: Review of PHP.

In covering PHP we have mentioned the following aspects.

Variables have names, like in Perl, and these names are always prefixed by the dollar sign ($).

So here's a simple PHP file:

    <title>PHP variables</title>
<body bgcolor=white>

  <? $i = 3; ?>

  <p> I have set <code>$i</code> to the value of <?=$i?>. </p> 

  <? $i += 2; ?>

  <p> Now I added 2 to it, so it has become <?=$i?>. </p>

Here's how it works.

Here's the source code, again.

What would be the basic Perl CGI script that does the same thing?

Perhaps something like this:


print "Content-type: text/html\n\n<html><body bgcolor=white>";

$i = 3; 

print "<p>I have set <code>\$i</code> to the value of $i.</p>";  

$i += 2; 

print "<p> Now I added 2 to it, so it has become $i. </p> "; 

print "</body></html>"; 

Here's this script in action.

Here's the version of it.


use CGI;

$query = new CGI; 

print $query->header; 

print $query->start_html(-bgcolor=>'white'); 

$i = 3; 

print qq{ 
  <p> I have set <code>\$i</code> to the value of $i. </p>

$i += 2;

print qq{ 
  <p> Now I have added 2 to it, so it has become $i.</p>

print $query->end_html;
Here it is in action.

Hashtables are used in Perl, and they are also used in PHP.

Here's some code in Perl,

%prices = ("Tires"       => 100,
           "Oil"         =>  10, 
           "Spark Plugs" =>  4); 

$prices{"Tires"} -= 90; 

foreach $key (keys %prices) {
  print $key, " --> ", $price{$key}, "<p>"; 
and here's how we would do the same thing in PHP.

Here's the code:

<html><body bgcolor=white>

<? $prices = array("Tires"       => 100, 
                   "Oil"         =>  10, 
                   "Spark Plugs" =>   4); 

   $prices["Tires"] -= 10;  
   while ($element = each ($prices)) {

     echo $element["key"] . " --> " . 
          $element["value"] . "<br>"; 



To do it twice, though, is a bit tricky.

Here's the source code for that. (Note we reset the array).

Now a word about sessions.

HTTP is connectionless, and keeping state with CGI requires

PHP provides the ability to manage state on the server side.

Here's a typical script.

Here's the source code.

Here's the source code again:

<? session_start();
   if (session_is_registered("acc")) { 
     if ($fun == "add") $acc += $arg; 
     else if ($fun == "sub") $acc -= $arg; 
   } else {
     $acc = 0; 

    <title>Sessions Examples</title>
  <body bgcolor=white>
    <form method="POST" action="<? echo $SCRIPT_NAME; ?>"> 

      The current value of the accumulator is: <? echo $acc ?> <p> 

      <table cellpadding=2>
          <td> Amount: </td> 
          <td> <input type="text" name="arg" size=4> </td> 
          <td> Function: </td> 
          <td> <select name="fun"> 
                 <option value="non"> Click Me! 
                 <option value="add"> Deposit
                 <option value="sub"> Withdraw   
      <p> Enter the amount, select a function, then press 
      <input type="submit" value="Proceed"> <p> 
The code in brown shows how values in the form fields can be retrieved. That's important, but not the central part of this example. Here's what gets created on /tmp if I work with this calculator a bit (which is what the example wants to bring forth): ls -ld /tmp/sess_05e56a5ed8be5e3a0ef771d9fb582df9
-rw-------   1 dgerman  faculty        10 Oct 17 14:44 /tmp/sess_05e56a5ed8be5e3a0ef771d9fb582df9 cat /tmp/sess_05e56a5ed8be5e3a0ef771d9fb582df9
My accumulator was 122 at the time I printed the file.

The name of the file contains the random session ID (in blue).

The contents of the file contains the server-side state (environment).

That is in red.

Note that for every browser that calls, a session ID is created.

The session ID is sent to the browser, and kept in a cookie, there.

(Other ways of storing the session ID on the browser are possible, too).

A file with the ownership of the server will then be created on the server.

Where will it be created?

Your config file (php.ini) determines that, and here's the line in it.

ifx.nullformat = 0

; Handler used to store/retrieve data.
session.save_handler = files

; Argument passed to save_handler.  In the case of files, this is the path
; where data files are stored.
session.save_path = /tmp

; Whether to use cookies.
session.use_cookies = 1

; Name of the session (used as cookie name). = PHPSESSID

; Initialize session on request startup.
session.auto_start = 0

To summarize:

That's the basic idea behind sessions.

Experiments done Monday-Tuesday with

and the servers of indicate that Internet Explorer and Netscape may store sessions ID somewhat differently.

We need to further investigate that, later.

We will cover cookies when we look at Javascript.

At that time we will look at cookies from three points of view:

In this last category we will have both Perl and PHP.

What do you need to turn in in the next lab?

Well, here now is your...



Modify your php.ini file to store session files in your account.

Choose /u/username/apache/apache_1.3.20/phpsessions for the new location.

Don't forget to create this folder first.


Read your php.ini file and document (other) interesting findings.

Write a report, call it Lab Seven Findings and post it.


Answer the following questions (in writing).

  1. Does it matter (does it make any difference) in the calculator program above if we encode the request method in the form as GET, as opposed to POST?

  2. Check any resources you have and determine how could one set, or influence the duration of time a session ID is valid (its life span). If the browser does not allow or support cookies, is session support lost?

Post these as well, call the document Lab Seven Extra Credit Questions.

Last updated on Oct 18, 2001, by Adrian German for A348/A548