PHP
  Home arrow PHP arrow Page 8 - Logging With PHP
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 
Dedicated Servers 
E-Commerce Hosting 
Linux Web Hosting 
Managed Hosting 
Small Business Hosting 
Actuate Whitepapers 
VeriSign Whitepapers 
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

Logging With PHP
By: icarus, (c) Melonfire
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 28
    2002-11-27

    Table of Contents:
  • Logging With PHP
  • The Bare Necessities
  • Turning The Tables
  • Turning Up The Heat
  • Biting Into A PEAR
  • Destination Unknown
  • Artificial Intelligence
  • Big Brother Is Watching
  • 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

    Stay one step ahead of the competition. Evaluate and give feedback on some of the hottest web development tools on the market today. Make your opinion heard! Click Here

    Logging With PHP - Big Brother Is Watching


    (Page 8 of 9 )

    Finally, let's wrap things up with a couple of examples that show how the various techniques demonstrated above can be used to build logs and audit trails for a Web application.

    In the first example, every time a Web page is displayed, a log entry is made in an "access.log" file. This log entry is a comma-separated list of values containing the URL requested, the client browser identification string, and a timestamp.

    <?php // create log string $str = date("Y/m/d h:i:s", mktime()) . "," . $_SERVER['REQUEST_URI'] . "," . $_SERVER['HTTP_USER_AGENT'] . "\n"; // write to file error_log($str, 3, "access.log"); // rest of page here ?>
    Here's a snippet from the access log:

    2002/11/26 09:07:38,/alpha.php,Mozilla/4.0 (compatible; MSIE 5.0; Windows 95) 2002/11/26 09:07:38,/alpha.php,Mozilla/4.0 (compatible; MSIE 5.0; Windows 95) 2002/11/26 09:10:31,/home/hello.php,Mozilla/4.0 (compatible; MSIE 5.0; Windows 95) 2002/11/26 09:17:38,/index.php,Mozilla/4.0 (compatible; MSIE 5.0; Windows 95) 2002/11/26 09:17:38,/base/index.php,Mozilla/4.0 (compatible; MSIE 5.0; Windows 95)
    Since this data is in a structured format, it can easily be analyzed and a "hit count" created for each URL in the file. This next script does exactly that:

    <?php // create array to hold unique URLs $urlStats = array(); // read access log $lines = file("access.log"); // iterate through access log foreach ($lines as $l) { $data = explode(",", $l); $ts = $data[0]; $url = $data[1]; $agent = $data[2]; // check to see if URL exists in array // if it does, increment incidence count // if it does not, create a new key with incidence count 1 if ($urlStats[$url]) { $urlStats[$url]++; } else { $urlStats[$url] = 1; } } // print list of unique URLs with count print_r($urlStats); ?>
    This script parses the "access.log" file, and creates a PHP associative array whose keys correspond to the URLs found in the file. The value associated with each key is an integer indicating the number of appearances the URL makes in the file. Once the entire file has been parsed, the $urlStats array contains a list of all the unique URLs in the access log, together with the number of times each has appeared. This data can then be used to generate a report of the most frequently-accessed URLs.

    Consider this next example, which provides an API for adding, editing and deleting users to (from) a Web application. Each time the user database is edited, a separate audit() process tracks the change, logging both the nature of the change and information about the user initiating the change. This log data is stored in a separate SQL table, from where it can be retrieved for statistical reporting, user activity monitoring or debugging.

    <?php // assume that administrator has logged in to system to perform user-administration tasks // admin username is stored in a session variable by default // this is useful for audit purposes session_start(); $_SESSION['LOGGED_IN_USER'] = "john"; // add a new user function addUser($user, $pass, $perms) { // open connection to database $connection = mysql_connect("localhost", "joe", "pass") or die ("Unable to connect!"); mysql_select_db("myapp") or die ("Unable to select database!"); // formulate and execute query $query = "INSERT INTO users (user, pass, perms) VALUES('$user', '$pass', '$perms')"; mysql_query($query) or die ("Error in query: $query. " . mysql_error()); // log activity to audit database audit("ADD_USER", $_SESSION['LOGGED_IN_USER'], "$user:$pass:$perms", addslashes($query)); // close connection mysql_close($connection); } // edit an existing user function updateUser($user, $pass, $perms) { $connection = mysql_connect("localhost", "joe", "pass") or die ("Unable to connect!"); mysql_select_db("myapp") or die ("Unable to select database!"); // formulate and execute query $query = "UPDATE users SET pass = '$pass', perms = '$perms' WHERE user = '$user'"; mysql_query($query) or die ("Error in query: $query. " . mysql_error()); // log activity to audit database audit("UPDATE_USER", $_SESSION['LOGGED_IN_USER'], "$user:$pass:$perms", addslashes($query)); // close connection mysql_close($connection); } // delete an existing user function deleteUser($user) { $connection = mysql_connect("localhost", "joe", "pass") or die ("Unable to connect!"); mysql_select_db("myapp") or die ("Unable to select database!"); // formulate and execute query $query = "DELETE FROM users WHERE user = '$user'"; mysql_query($query) or die ("Error in query: $query. " . mysql_error()); // log activity to audit database audit("DELETE_USER", $_SESSION['LOGGED_IN_USER'], "$user", addslashes($query)); // close connection mysql_close($connection); } // generic audit function // logs all activity to a database function audit($op, $owner, $args, $msg) { $connection = mysql_connect("localhost", "root", "pass") or die ("Unable to connect!"); mysql_select_db("trails") or die ("Unable to select database!"); // formulate and execute query $query = "INSERT INTO audit (timestamp, op, owner, args, msg) VALUES (NOW(), '$op', '$owner', '$args', '$msg')"; mysql_query($query) or die ("Error in query: $query. " . mysql_error()); } addUser("joe", "joe", 3); addUser("sarahh", "bsdfg49", 1); updateUser("joe", "joe", 4); deleteUser("sarahh"); addUser("sarah", "bsdfg49", 1); ?>
    Here's a snippet from the audit table:

    +---------------------+-------------+-------+ | timestamp | op | owner | +---------------------+-------------+-------+ | 2002-11-26 08:28:05 | UPDATE_USER | john | | 2002-11-26 08:28:05 | DELETE_USER | john | | 2002-11-26 08:28:05 | ADD_USER | john | | 2002-11-26 08:33:14 | ADD_USER | joe | +---------------------+-------------+-------+
    This audit table can then be queried to obtain detailed information on the activities performed by the various users, sorted by time or type of activity. For example,

    mysql> SELECT timestamp, op, args FROM trails WHERE timestamp >= mysql> 2002-11-26 AND owner = 'joe'; +---------------------+-------------+------------------+ | timestamp | op | args | +---------------------+-------------+------------------+ | 2002-11-26 08:33:29 | ADD_USER | joe:joe:3 | | 2002-11-26 08:33:29 | ADD_USER | sarahh:bsdfg49:1 | | 2002-11-26 08:33:29 | UPDATE_USER | joe:joe:4 | | 2002-11-26 08:33:29 | DELETE_USER | sarahh | | 2002-11-26 08:33:29 | ADD_USER | sarah:bsdfg49:1 | +---------------------+-------------+------------------+
    This is a somewhat trivial example, but it serves to demonstrate the concept of logging activity and using those logs to build an audit trail. While you can make this as complex as you want, tracking everything from user clicks to form input in order to gain a better understanding of how users navigate through and use your application, remember that every addition to the log affects the overall performance of your application; log too much data and your application will suffocate and die.

    More PHP Articles
    More By icarus, (c) Melonfire


       · I like the simple example that loggs to a file and with a small modification, we can...
       · This is a great stuff...But how can we make the log file thus created write...
       · This should do it:[code]function log_action($msg) { $today =...
       · This should do it:[code]function log_action($msg) { $today =...
     

       

    PHP ARTICLES

    - Viewing and Editing Tasks for a Project Mana...
    - More on Private Methods with PHP 5 Member Vi...
    - Adding Tasks to a Project Management Applica...
    - Utilizing Private Methods with PHP 5 and Mem...
    - Making Changes in a Project Management Appli...
    - Defining Public and Protected Methods with M...
    - HTML for a Project Management Application
    - Using Subclasses and Accessors with Member V...
    - Implementing Internet Protocols with PHP
    - Project Management: The Application
    - Working with Private Properties to Protect P...
    - Protecting PHP 5 Class Data with Member Visi...
    - Setting Up a Web-based Image Hosting Service
    - Comparing Files and Databases with PHP Bench...
    - Setting Up a Web-Based Image Gallery





    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 1 hosted by Hostway