Testing first prototype


13/8/02
 

Comparing dates

Before testing the script I naturally tried to think my way through it as carefully as I could. I spotted and corrected several typos (which are shown corrected on the previous page), but I realised that there is one bug which I think would cause failures to occur only on certain days. In the PHP I'm comparing dates like this...
if ( $date_of_file < $today ) {
...but that will do an alphabetical comparison rather than a proper comparison. It might give the right result when comparing, say, 28/08/02 and 27/08/02 but would give the wrong result when comparing 01/09/02 with 27/08/02! I think I can fix this fairly easily by converting dates to days elapsed relative to the "UNIX epoch", but I intend to leave that for later because I think it shouldn't stop the script from working for the remainder of this month.

Creating a test version

Given the complexity of the script I can't believe it's going to work first time, so I'm going to build in some echo statements so that I can see how far things are progressing:
<?php

// "Expiry Checker"

// A script to automatically remind FaCES web page owners...
// ...when their web pages are older than their expiry dates.

// author = Martin Bush, South Bank University
// date = 13 August 2002
// filename = expirychecker.php3
// version = 1st prototype, 2nd version
// history = as v1.1, plus some diagnostic echo statements

// Comments in quotations are inherited from the pseudocode

// "read Expiry Checker information (url&expiry&owner&message)"
// "parse the information using the parse_str function"
parse_str($QUERY_STRING);  // defines $url, $expiry, $owner, $message

echo "This should be the url...   " . $url;
echo "\nThis should be the expiry date...   " . $expiry;
echo "\nThis should be the owner...   " . $owner;
echo "\nThis should be the message...   " . $message;

// "if (expiry_date <= today's date) then"
$today = date ("d/m/y")

echo "\nThis should be today's date...   " . $today;

if ( $today <= $expiry ) {

  // "lock file reminders_sent_today.txt" -- abbreviate to "reminders"
  $reminders_file = "reminders_sent_today.txt";  //file is assumed to exist
  // need to open reminders file first -- "r+" means for reading and writing
  $reminders_file_pointer = fopen($reminders_file, "r+");
  // now lock reminders file to ensure mutually exclusive access
  $lock = flock($reminders_file_pointer, 2); // "2" for exclusive writing lock
  // continue when lock is obtained
  if ($lock) {

    // "read date_of_file from file" -- assumed to be present and valid
    $date_of_file = fgets($reminders_file_pointer, 8); // reads up to 8 chars

echo "\nThis should be the date_of_file...   " . $date_of_file;

    // "if (date_of_file < today's date) then"
    if ( $date_of_file < $today ) {

      // "rewind file"
      rewind($reminders_file_pointer);

      // "write today's date to file"
      fwrite($reminders_file_pointer, "$today\n");

    }  // "endif"

    // "search file for url of web page" -- stored in alphabetical order
    $found = 0;
    //  get next entry if there is one
    if (!feof($reminders_file_pointer)) {
      $next_entry = fgets($reminders_file_pointer, 256);

echo "\nThis should be the first Expiry Checker entry...   " . $next_entry;

      // "url" in web page corresponds to "filed_url" in reminders file...
      //  ...and likewise for "expiry", "owner" and "message"
      parse_str($next_entry);  // defines $filed_url, filed_expiry, etc.

echo "\nThese should be its constituants...   ";
echo "\n..." . $filed_url;
echo "\n..." . $filed_expiry;
echo "\n..." . $filed_owner;
echo "\n..." . $filed_message;

      // while loop stops if it goes past the point where $url should be
      while (!feof($reminders_file_pointer) & (($filed_url <= $url)
                                                      & ($found == 0))) {
        if ($filed_url == $url) {
          $found = 1;
        } else {
          // accumulate entries read so far, and get next entry
          $file_contents_so_far = $file_contents_so_far.$next_entry;
          $next_entry = fgets($reminders_file_pointer, 256);
        }
      }
		}

    // "if (URL not found in file) then"
    if ($found == 0) {

      // "write URL to file"
      // must carry on reading contents first
      $file_contents_remainder = fread($reminders_file_pointer,
                                       filesize($reminders_file));
      // now rewind the file
      rewind($reminders_file_pointer);
      // write accumulate entries up to point of insertion
      fwrite($reminders_file_pointer, "$file_contents_so_far\n");
      // prepare new reminder entry
      $new_reminder = "filed-url=".$url."&filed-expiry=".$expiry.
               "&filed-owner=".$owner."&filed-message=".$message;
      // insert new reminder entry
      fwrite($reminders_file_pointer, "$new_reminder\n");
      // write remainder
      fwrite($reminders_file_pointer, "$file_contents_remainder");

      // "email URL and message to owner"
      $mail_message = "This message has been sent automatically,
      \nto remind you that the contents of this FaCES web page...
      \n    @url
      \nnow needs your attention.
      \n\nHere is the message that accompanied your email address...
      \n    @message";
      mail($owner, "Expiry Checker: FaCES web page needs your attention.",
      $mail_message)

    }  // "endif"

  }  // endif ($lock)
  // "unlock file reminders_sent_today.txt"
  $lock = flock($respondees_file_pointer, 3); // "3" means unlock
  // need to close respondees file
  fclose($respondees_file_pointer);

}  // "endif" ( $today <= $expiry )

?>
 
 
 

<<contents ^top^ next>>