CSC 325 | Grinnell College | Fall, 2008 |
Databases and Web Application Design | ||
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:
Consider the statement.
$month = array ('January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December');
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.
Place the months in alphabetical order with the statement
sort($month)
and print the months $month[0], ..., $month[12] again in a loop.
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>"; }
Add the above foreach statement to your HTML/PHP script. What is the purpose of the line echo "<br>"?
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);
Print the elements of this array with the loop:
foreach ($monthDays as $item => $days) { echo $item . ":" . $days; echo "<br>"; }
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.
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.
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
Copy this script to your account, and set its permissions so that it can be viewed in your browser.
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.
In the printing in the third section, write a paragraph describing how the code generates the obtained retrieved.
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.
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.
Follow the directions in the departmental documentation on SSH, so that you can log into the Web server aiken or, more completely, aiken.cs.grinnell.edu.
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.
Use ssh to log into aiken.cs.grinnell.edu, and then change the active directory (cd) to /var/log/apache2.
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.
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 132.161.196.123.
Several utilities provide information about workstations associated with IP addresses.
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
nslookup 132.161.196.123 whois 132.161.196.123
Describe the result of each query.
A few Web sites give additional information about the location of IP addresses:
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?
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?
This document is available on the World Wide Web as
http://www.walker.cs.grinnell.edu/courses/325.fa08/lab-php-arrays.shtml
created 10 August 2008 last revised 19 September 2008 |
![]() ![]() |
For more information, please contact Henry M. Walker at walker@cs.grinnell.edu. |