MySQL
  Home arrow MySQL arrow Page 2 - Logging with PHP
Dev Articles Forums 
ADO.NET  
Apache  
ASP  
ASP.NET  
C#  
C++  
ColdFusion  
COM/COM+  
Delphi-Kylix  
Design Usability  
Development Cycles  
DHTML  
Embedded Tools  
Flash  
Graphic Design  
HTML  
IIS  
Interviews  
Java  
JavaScript  
MySQL  
Oracle  
Photoshop  
PHP  
Reviews  
Ruby-on-Rails  
SQL  
SQL Server  
Style Sheets  
VB.Net  
Visual Basic  
Web Authoring  
Web Services  
Web Standards  
XML  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
Sun Developer Network 
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? 
MYSQL

Logging with PHP
By: Tim Perdue
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 2
    2003-02-06

    Table of Contents:
  • Logging with PHP
  • The Design
  • Source Code
  • Conclusion

  • 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


    Logging with PHP - The Design


    (Page 2 of 4 )

    I sat down and came up with the following table:

    create table tbl_activity_log (
    fld_date int, //ex. 19990101
    fld_hours text, //Which hour of the day?
    fld_remote_ip text, //IP of visitor
    fld_action int, //page view, click, etc
    fld_special text, //which page or category?
    fld_affil_num int //unique for each web site
    );


    Someone who really knows what they're doing would have used a timestamp field rather than int fld_date, but this works for me. You could also add another column to hold the $PATH_INFO, but I decided the Special column gives me what I want.
    I also defined these indexes, as I knew I would be querying on any of these fields:

    INDEXES
    idx_logger_date
    idx_logger_hour
    idx_logger_ip
    idx_logger_action
    idx_logger_special
    idx_logger_affil_num


    The next step was getting the info into the tables. It would be a perfect world if all pages were served through PHP and all of your various web sites existed on one box. Unfortunately, I have an array of servers scattered all over the country, and I want to collect this info from every server for every page view 24 hours a day.

    So that required me to use the 1x1 pixel GIF trick. I have a GIF on every page that looks like this:

    <IMG SRC="http://www.yourserver.com/util/gif11.php?c=4&s=phpbuildercom&b=77" height=1 width=1>

    gif11.php is a simple script that resides on my central server. I have included the source on the next page. Since the gif is on each page, and is forced to load due to the random number at the end (the b=xxxx), a request is sent back to the central server for each and every page view.

    If all of your pages and the database reside on one server, you don't need to use the GIF trick - you can insert the logging code into the header of your page.
    The same logging system can be used to track click-thrus on ad banners:

    <A HREF="http://www.yourserver.com/util/adclick.php?c=4&goto= * >

    Just replace * with the URL you want to redirect to. Again here, the $c variable should be changed for each web site.

    Reporting

    Reporting all this information is easy with PHP - just write up some SQL, execute it and use the ShowResults class to display it.

    So now the system is in place and I can run a report at any time and get statistics up to the minute for any given site.

    Here's a sample report from Friday on PHPBuilder.com:

    fld_datefld_specialimpressions
    19990129forum267
    19990129mail559
    19990129manual397
    19990129phpbuildercom1820
    19990129px431
    19990129siteforum58


    Overall, I'm pretty impressed with this system. There's really no limit to its flexibility - in fact, I am expanding it to keep track of ad banners, and I may write up an article on that later.

    Please use the sample code on the next page and tell me how you used it, or if you added anything interesting to it.

    More MySQL Articles
    More By Tim Perdue


     

    MYSQL ARTICLES

    - MySQL and BLOBs
    - Two Lessons in ASP and MySQL
    - Lord Of The Strings Part 2
    - Lord Of The Strings Part 1
    - Importing Data into MySQL with Navicat
    - Building a Sustainable Web Site
    - Creating An Online Photo Album with PHP and ...
    - Creating An Online Photo Album with PHP and ...
    - PhpED 3.2 – More Features Than You Can Poke ...
    - Creating An Online Photo Album with PHP and ...
    - Creating An Online Photo Album with PHP and ...
    - Security and Sessions in PHP
    - Setup Your Personal Reminder System Using PHP
    - Create a IP-Country Database Using PERL and ...
    - Developing a Dynamic Document Search in PHP ...






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