· Martin's Expiry Checker v3 Blog ·

M.E.Bush > Misc. > Expiry Checker v3 Blog > 17-Aug-2006

The easy part - now done using MySQL

First I wanted to make sure that I could get the Expiry Checker simply to write expired page entries to the database without duplication. This seems to work...
<?php

function connect_to_db() {
  if ( $db = @mysql_connect( "mysql.lsbu.ac.uk", "bushm", "****")) {
    if ( @mysql_select_db( "bushm", $db)) {
      return $db;
    } else {
      die( "<h1>Failed to obtain access to database!</h1>");
    }
  } else {
    die( "<h1>Failed to connect to database server!</h1>");
  }
}

//----------------------------------------------------------------

function TruncateURL($theURL) {

// Truncate URL if it ends with a "?", and then truncate it further
// - if necessary - to remove "/index.html", "/index.shtml" or "/"

  if (substr($theURL,-strlen("?"))=="?") {
    $theURL = substr($theURL,0,-strlen("?"));
  }
  if (substr($theURL,-strlen("/index.html"))=="/index.html") {
    $theURL = substr($theURL,0,-strlen("/index.html"));
  }
  if (substr($theURL,-strlen("/index.shtml"))=="/index.shtml") {
    $theURL = substr($theURL,0,-strlen("/index.shtml"));
  }
  if (substr($theURL,-strlen("/"))=="/") {
    $theURL = substr($theURL,0,-strlen("/"));
  }
  return($theURL);
}

//----------------------------------------------------------------

function GetTodayYYMMDD() {

  // Get today's date in dd/mm/yy format
  $today = date("d/m/y");
  // Convert to yymmdd format - e.g. 24/08/02 becomes 020824
  // *** End-of-century bug - will fail in the year 2100! ***
  $today_day = substr($today,-8,2);
  $today_month = substr($today,-5,2);
  $today_year = substr($today,-2,2);
  $today_yymmdd = $today_year.$today_month.$today_day;
  return($today_yymmdd);
}

//----------------------------------------------------------------

function ConvertDateToYYMMDD($theDate) {

  // prepare $theDate for conversion
  // handle dd/mm/yy dates including single digits for dd, mm, yy
  // - e.g. 06/06/03, 6/6/03, 06/6/03, 6/06/03, 06/06/3 etc.
  $position_of_first_slash = strpos($theDate, "/");
  $expiry_day = substr($theDate, 0, $position_of_first_slash);
  $expiry_mm_yy = substr($theDate, $position_of_first_slash + 1, 
                                                strlen($theDate));
  $position_of_second_slash = strpos($expiry_mm_yy, "/");
  $expiry_month = substr($expiry_mm_yy, 0, $position_of_second_slash);
  $expiry_year = substr($expiry_mm_yy, $position_of_second_slash + 1, 
                                                strlen($theDate));
  // if any of dd, mm or yy are single digits then add a leading zero
  if ( strlen($expiry_day) == 1 ) { 
    $expiry_day = "0".$expiry_day; 
  }
  if ( strlen($expiry_month) == 1 ) {
    $expiry_month = "0".$expiry_month; 
  }
  if ( strlen($expiry_year) == 1 ) {
    $expiry_year = "0".$expiry_year; 
  }
  // now able to convert expiry date to yymmdd format
  $expiry_yymmdd = $expiry_year.$expiry_month.$expiry_day;

  // if $theDate contained no "/"s then it was invalid, in which case
  // return zero (this is important for the Expiry Checker)
  if ($position_of_first_slash == 0) {
    return(0);
  } else {
    return($expiry_yymmdd);
  }
}

//================================================================


/*
// MAIN PROGRAM
*/


// Discover url of referring web page
$url = $_ENV['HTTP_REFERER'];

// Get $owner, $expirydate and $message from referring web page
$owner = $_GET['owner'];
if ($owner == "") {
  $owner = "bcim@lsbu.ac.uk";
}
$expirydate = $_GET['expirydate'];
$message = $_GET['message'];

// if today is not a Sunday or a Monday then
$day_of_week = time("D");
if (!($day_of_week == "Sun") && !($day_of_week == "Mon")) {
  
  // get $expirydate from referring web page
  $expirydate = $_GET['expirydate'];

  // use ConvertDateToYYMMDD to re-format $expirydate
  $expirydateYYMMDD = ConvertDateToYYMMDD($expirydate);

  // get today's date in YYMMDD format
  $todayYYMMDD = GetTodayYYMMDD();

  // if the referring web page has expired then
  if ($expirydateYYMMDD <= $todayYYMMDD) {

    // if the page is an LSBU WWW or MYWEB page then
    if (((substr($url, 0, 21) == "http://www.lsbu.ac.uk"))
        or ((substr($url, 0, 23) == "http://myweb.lsbu.ac.uk"))) {

      // normalise the page url
      $url = TruncateURL($url);
    
      // add page to expirydataLIVE, avoiding duplicates
      $db = connect_to_db();
      $sql = "SELECT * FROM expirydataLIVE WHERE url=\"$url\"";
      $result = mysql_query($sql, $db);
      if (!$result) die("<h1>Query Failed!</h1>");
      if (mysql_num_rows($result) == 0) {
        $sql = "INSERT INTO expirydataLIVE SET url=\"$url\", ";
        $sql .= "expirydate=\"$expirydate\", owner=\"$owner\", ";
        $sql .= "message=\"$message\"";
        $result = mysql_query($sql, $db);
        if (!$result) die("<h1>Query Failed!</h1>");
      }

    }  // endif (the page was an LSBU WWW or MYWEB page)

  }  // endif (the page had expired)
  
}  // (today wasn't a Sunday or a Monday)

?>