PHP
  Home arrow PHP arrow Making Usage Statistics in 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 
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

Making Usage Statistics in PHP
By: Adam Szanto
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 121
    2004-02-16

    Table of Contents:
  • Making Usage Statistics in PHP
  • Save the HTML Title
  • Showing the Statistics Table
  • Showing Figures Based on the Statistics
  • Creating the Image Library
  • Charting the Results
  • Before Saying Goodbye

  • 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


    Making Usage Statistics in PHP


    (Page 1 of 7 )

    Making usage statistics on a web site is one of the most enjoyable things for me on the Internet. Thanks to the technology, you can see each click of each visitor, the date of the visiting, and how many seconds the user was reading your site. I use this feature to track which of my articles was read for the longest time. In this article I will show you how to store the statistics in a MySQL database, show the web stats in an HTML table and make figures based on the stats using the GD library. 

    Making Usage Statistics in PHP - Storing the Data
    Let's start with the database part. We'll need only one table that stores each click (page impression) of each visitor. The name of the table will be "hits". What should we store here?

    1. The hostname is very important to identify the client. PHP gives us the IP address that I like to convert to hostname with gethostbyaddr(). I think it's better to see a string.
    2. The time of visit can be stored easily by using the NOW() function of MySQL.
    3. The visited page on your site should be stored. In most cases, you should store the URL of the visited page and the HTML title in two separated fields, eg. URL and Title fields, so that you'll be able to see your most visited sites simply by looking through the list of their titles.

    Now we're going to write the piece of code that will save the hit. It will be called at the beginning of the program. To save the requested URL in the database you can use $_SERVER['PHP_SELF'] that gives you the URL without the domain name or $_SERVER['QUERY_STRING'] that gives you the parameters in the URL after the question mark.

    Warning: $_SERVER['PHP_SELF'] doesn't contain the query string, only the path related to the server root and the filename. For instance if you call http://www.mydomain.com/folder1/personal/main.php?id=56 then PHP_SELF is /folder1/personal/main.php.

    I prefer $_SERVER['QUERY_STRING'].  This can be especially useful if you use only one PHP to all functions, for example, you call "/index.php?op=forum&topicid=768" if you'd like to show a given forum topic or for showing articles /index.php?op=articles&id=25. Using QUERY_STRING, You will get "op=articles&id=25" that will be enough to identify the requested page.

    More PHP Articles
    More By Adam Szanto


     

    PHP ARTICLES

    - Making Usage Statistics in PHP
    - Installing PHP under Windows: Further Config...
    - File Version Management in PHP
    - Statistical View of Data in a Clustered Bar ...
    - Creating a Multi-File Upload Script in PHP
    - Executing Microsoft SQL Server Stored Proced...
    - Code 10x More Efficiently Using Data Access ...
    - A Few Tips for Speeding Up PHP Code
    - The Modular Web Page
    - Quick E-Commerce with PHP and PayPal
    - Regression Testing With JMeter
    - Building an Iterator with PHP
    - PHP Frontend to ImageMagick
    - Using PEAR's mimeDecode Module
    - Incoming Mail and PHP







    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 4 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek