PHP
  Home arrow PHP arrow Page 2 - Watching The Web
Dev Shed Forums 
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Forums Sitemap 
IBM® developerWorks 
Sun Developer Network 
E-Commerce Hosting 
Linux Web Hosting 
Managed Hosting 
Small Business Hosting 
Mobile Linux 
App Generation ROI 
VPS Hosting 
Weekly Newsletter

 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
PHP

Watching The Web
By: The Disenchanted Developer, (c) Melonfire
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 6
    2002-10-23

    Table of Contents:
  • Watching The Web
  • Code Poet
  • Digging Deep
  • Backtracking
  • Plan B
  • Closing Time

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    Watching The Web - Code Poet


    (Page 2 of 6 )

    With the theory out of the way, I was just about ready to make my first stab at the code. Since I was told that there are a large number of URLs to be monitored, I decided to use a MySQL database table to store them, in addition to a brief description of each URL. The table I came up with is pretty simple - here's what it looked like:

    CREATE TABLE urls (
    id tinyint(3) unsigned NOT NULL auto_increment,
    url text NOT NULL,
    dsc varchar(255) NOT NULL default '',
    date datetime default NULL,
    email varchar(255) NOT NULL default '',
    PRIMARY KEY (id)
    );
    And here's a sample of the data within it:
    mysql> select * from urls;
    +----+---------------------------------+----------------+------+--------
    +----+---------------------------------+----------------+------+----
    ------+
    | id | url                             | dsc            | date | email
    |
    +----+---------------------------------+----------------+------+--------
    +----+---------------------------------+----------------+------+----
    ------+
    |  1 | http://www.melonfire.com/       | Melonfire.com  | NULL |
    user@some.domain |
    |  2 | http://www.yahoo.com/           | Yahoo.com      | NULL |
    user@some.domain |
    |  3 | http://www.devshed.com/         | Devshed.com    | NULL |
    user@some.domain |
    +----+---------------------------------+----------------+------+--------
    +----+---------------------------------+----------------+------+----
    ------+
    3 rows in set (0.00 sec)
    Next up, I needed a script that would iterate through this database table, connect to each of the URLs listed within it, and obtain the value of the "Last-Modified" header - basically, replicate what I did with my telnet client, as many times as there were URLs. Here's what I put together:
    <?php
    // DB connection parameters
    $db_host="localhost";
    $db_user="joe";
    $db_pass="65h49";
    $db_name="db167";
    // open database connection
    $connection = mysql_connect($db_host, $db_user, $db_pass) or die
    ("Unable to connect!"); mysql_select_db($db_name);
    // generate and execute query
    $query1 = "SELECT id, url, date, dsc, email FROM urls"; $result1 =
    mysql_query($query1, $connection) or die ("Error in query: $query1 . " .
    mysql_error());
    // if rows exist
    if (mysql_num_rows($result1) > 0)
    {
    // iterate through resultset
    while(list($id, $url, $date, $desc, $email) =
    mysql_fetch_row($result1))
    {
    $response = "";
    // parse URL into component parts  
    $arr = parse_url($url);
    // open a client connection
    $fp = fsockopen ($arr['host'], 80);
    // send HEAD request and read response
    $request = "HEAD /" . $arr['path'] . "
    HTTP/1.0\r\n\r\n";
    fputs ($fp, $request);
    while (!feof($fp)) 
    {
    $response .= fgets ($fp, 500);
    }
    fclose ($fp);
    // split response into lines
    $lines = explode("\r\n", $response);
    // scan lines for "Last-Modified" header
    foreach($lines as $l)
    {
    if (ereg("^Last-Modified:", $l)) 
    {
    // split into variable-value component
    $arr2 = explode(": ", $l);
    $newDate = gmdate("Y-m-d H:i:s",
    strtotime($arr2[1]));
    // if date has changed from
    last-recorded date
    if ($date != $newDate)
    {
    // send mail to owner 
    mail($email, "$desc has
    changed!", "This is an automated message to inform you that the URL
    \r\n\r\n $url \r\n\r\nhas changed since it was last checked. Please
    visit the URL to view the changes.", "From: The Web Watcher
    <nobody@some.domain>") or die ("Could not send mail!");
    // update table with new date
    $query2 = "UPDATE urls SET date
    = '" . $newDate . "' WHERE id = '" . $id . "'";
    $result2 = mysql_query($query2,
    $connection) or die ("Error in query: $query2 . " . mysql_error());
    } 
    } 
    }
    } 
    }
    // close database connection
    mysql_close($connection);
    ?>
    How does this work? Let's look at that next.

    More PHP Articles
    More By The Disenchanted Developer, (c) Melonfire


     

       

    PHP ARTICLES

    - Using Aliases and the Autoload Function with...
    - Authentication Scripts for a User Management...
    - Utilizing the Use Keyword for Namespaces in ...
    - Building a User Management Application
    - Working With Different Namespaces in PHP 5
    - User Management Explained: Overview
    - Using Namespaces in PHP 5
    - Building a Modular Exception Class in PHP 5
    - Database and Password Security for Web Appli...
    - Handling MySQL Data Set Failures in PHP 5
    - Building Site Registration for Web Applicati...
    - Intercepting Customized Exceptions in PHP 5
    - Sub Classing Exceptions in PHP 5
    - Building a Content Management System with Co...
    - Filters and Login Systems for Web Applicatio...

     
    Application Delivery: Everything You Wanted to Know, but Didn`t Know You Needed to Ask
    A comprehensive guide to examining the topics of Wide-area Data Services and app....

     
    Best Practices: Safe and Secure Hardware Asset Recovery
    Companies increasingly must meet EPA and local requirements for the disposal of ....

     
    Managing SSL Security in Multi-Server Environments
    Read this white paper to learn how to simplify management of your organization's....

     
    Open Source Security Myths
    Open Source Software (OSS) is computer software whose source code is available t....

     
    Power and Cooling Capacity Management for Data Centers
    This paper describes the principles for achieving power and cooling capacity man....

     




    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 6 hosted by Hostway
    Stay green...Green IT