CSC 325 Grinnell College Fall, 2008
Databases and Web Application Design

Laboratory Exercise on PHP Arrays and Log Files

PHP arrays represent a curious hybrid of index arrays (as in Java or C) and hash tables (called associative arrays in PHP). This laboratory exercise provides experience with both aspects of PHP arrays.

This laboratory exercise also explores log files kept by the CS/Math/Stat Web server.

Basic references for this lab include:

Indexed Arrays

Consider the statement.

   $month = array ('January', 'February', 'March', 'April',
                   'May', 'June', 'July', 'August',
                   'September', 'October', 'November', 'December'); 

Steps for this Lab

  1. Include the above $month assignment statement in a HTML/PHP script. With 12 elements in the array, the months can retrieved as $month[0], ..., $month[12]. Write a for loop that prints these months in order.

  2. Place the months in alphabetical order with the statement


    and print the months $month[0], ..., $month[12] again in a loop.

The foreach Statement

The foreach statement provides another way to cycle through the values in an array, when the index of each item is not of particular interest:

   foreach ($month as $item) {
      echo $item ;
      echo "<br>";

More Steps

  1. Add the above foreach statement to your HTML/PHP script. What is the purpose of the line echo "<br>"?

Associative Arrays

Consider the following array initializations:

   $monthDays = array ('January'   => 31, 'February' => 28,
                       'March'     => 31, 'April'    => 30,
                       'May'       => 31, 'June'     => 30, 
                       'July'      => 31, 'August'   => 31,
                       'September' => 30, 'October'  => 31, 
                       'November'  => 30, 'December' => 31); 

More Steps

  1. Print the elements of this array with the loop:

       foreach ($monthDays as $item => $days) {
          echo $item . ":" . $days;
          echo "<br>";
  2. Refer to the PHP documentation to identify the functions that determine the minimum and maximum values within an array. Apply these functions to the $monthDays array and print these values.

Environmental Variables

The lab on dynamic Web pages introduced the variables $_GET and $_POST to provide access to data from Web forms. After reading about associative arrays, it should be clear that $_GET and $_POST are, in fact, associative arrays.

Whenever a Web page is retrieved, the browser sends some preliminary information to the Web server, describing something of the user's environment. These data are available in the $_SERVER variable. Various fields within this variable are available in the PHP Manual

Now, consider the script env-variables.php.

Still More Steps

  1. Run this script. Using the PHP Manual, annotate at least 6 of the items printed.

The script itself may be found at ~walker/public_html/courses/325.fa08/labs/env-variables.php

  1. Copy this script to your account, and set its permissions so that it can be viewed in your browser.

  2. Review the first section of this program, and write a paragraph describing how this code generates the output retrieved.

The second section of this program starts with the assignment:

    $MyServer = $_SERVER;

After printing the array fields, array $MyServer is sorted.

  1. In the printing in the third section, write a paragraph describing how the code generates the obtained retrieved.

  2. Compare the results in the fourth section with the output obtained in the earlier sections. Write a paragraph describing why you obtain this result (rather than the output obtained in the third section.

Log in into Workstations with the Secure Shell ssh

The next part of this lab examines log files, kept by the department's Web server aiken for all accesses to files over the Web.

  1. Follow the directions in the departmental documentation on SSH, so that you can log into the Web server aiken or, more completely,

Examining Web Logs

A Web server typically logs basic information about each page that is accessed. On the CS/Math/Stat network, Web server aiken stores this information in directory /var/log/apache2.

  1. Use ssh to log into, and then change the active directory (cd) to /var/log/apache2.

  2. List the files in this directory with the command ls -l, and note how large the various files are.

To explain these files:

Information recorded in these logs is described in some detail in the Apache Server Log Files documentation.

From Step 13, you know that these log files can be extremely large indeed, so typically we use a filter to limit our information to specific files. In particular, the grep utility scans input for identified patterns.

  1. This lab is called lab-php-arrays.shtml. Find all references this week to this file using the command:

        cat access.log | grep lab-php-arrays.shtml

    Paste the last part of this listing into a text document for future reference.

In addition to file name information, the first part of the line indicates the Internet address (IP address) of the user. For example, the workstation in my office has IP address

Several utilities provide information about workstations associated with IP addresses.

  1. In a terminal window, use nslookup and whois to find the name of some local workstations which have displayed this lab during the current week. For example, type


    Describe the result of each query.

  2. A few Web sites give additional information about the location of IP addresses:

    1. Geobytes maintains an IP Address Locator tool.

    2. IP2Location maintains another IP Address Locator tool.

    Describe the information obtained from each of these sources. Also, in reviewing these pages, how do you think the address locators knew that you were accessing the pages from Grinnell College?

  3. Write a paragraph describing how public or private the reading of Web pages might be. To what extent can access to a Web page be traced to a specific individual?

Work to Turn In

This document is available on the World Wide Web as

created 10 August 2008
last revised 19 September 2008
Valid HTML 4.01! Valid CSS!
For more information, please contact Henry M. Walker at