PHP
  Home arrow PHP arrow Page 4 - Tracking Website Statistics with PHP
Dev Shed Forums  
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Smartphone Development  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Mobile Linux  
App Generation ROI  
IBM® developerWorks  
Forums Sitemap  
E-Commerce Hosting  
Linux Web Hosting  
Managed Hosting  
Small Business Hosting  
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? 
Google.com  
PHP

Tracking Website Statistics with PHP
By: Jacques Noah
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 24
    2006-09-12


    Table of Contents:
  • Tracking Website Statistics with PHP
  • Analyzing the Recorded Data
  • Visitor Specific Analysis
  • Building a Page

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      error-file:tidyout.log Del.ici.ous error-file:tidyout.log Digg
      error-file:tidyout.log Blink error-file:tidyout.log Simpy
      error-file:tidyout.log Google error-file:tidyout.log Spurl
      error-file:tidyout.log Y! MyWeb error-file:tidyout.log 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


    Tracking Website Statistics with PHP - Building a Page
    ( Page 4 of 4 )

    So, let's build a PHP page to display this information. Create a new PHP document, call it index.php and add the following code:

    Script: index.php

    <?
    include "trdbcon.php";
    ?>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-
    8859-1" />
    <title>Untitled Document</title>
    <style>
    .{
    font-size:12px;}
    </style>
    </head>
    <body>
    <strong>WebSite Statistics</strong>
    <br />
    <table width="100%" border="1" cellspacing="1" bordercolor="#FF0000">
      <tr>
        <td width="16%"><strong>No. Visitors </strong></td>
        <td width="24%"><strong>Date Visited </strong></td>
        <td width="25%"><strong>Browsers Used </strong></td>
        <td width="35%"><strong>Pages Visited </strong></td>
      </tr>
      <?
      $query = "SELECT *,COUNT(*) FROM stattracker GROUP by ip";
    $result=mysql_query($query);
    $number_of_views = mysql_num_rows($result);
    if($number_of_views>0){
    while($row=mysql_fetch_assoc($result)){ 

      ?>
      <tr valign="top">
        <td><?=$number_of_views;?></td>
        <td><?=$row['date_visited'];?></td>
        <td><?=$row['browser'];?></td>
        <td><?=$row['page'];?></td>
      </tr>
      <? }
      }else{
       ?>
      <tr><td><p>No Results</p></td></tr>
      <? }?>
    </table>
    <br />
    <strong>Visitor Statistics</strong>
    <br />
    <table width="100%" border="1" cellspacing="1" bordercolor="#FF0000">
      <tr>
        <td width="16%"><strong>Visitors ID </strong></td>
        <td width="24%"><strong>Date Visited </strong></td>
        <td width="25%"><strong>Browser Used </strong></td>
        <td width="35%"><strong>Pages Visited </strong></td>
      </tr>
       <?
     $queryV = "SELECT *,COUNT(*) FROM stattracker WHERE ip =
    '127.0.0.1' GROUP by page";
    $resultV=mysql_query($queryV);
    $visitors = mysql_num_rows($resultV);
    if($visitors>0){
    while ($rowV =mysql_fetch_assoc($resultV)) {
    //echo "Visited " .$row['count(*)']. " times";

      ?> 

      <tr valign="top">
        <td><?=$rowV['ip'];?></td>
        <td><?=$rowV['date_visited'];?></td>
        <td><?=$rowV['browser'];?></td>
        <td><?=$rowV['page'];?></td>
      </tr>
      <? }
      }else{
       ?>
      <tr><td><p>No Results</p></td></tr>
      <? }?>
    </table>
    <br />
    <strong>Page Statistics</strong>
    <br />
    <table width="100%" border="1" cellspacing="1"
    bordercolor="#FF0000" >
      <tr>
        <td width="16%"><strong>Page Name </strong></td>
        <td width="24%"><strong>No. Visits </strong></td>
        <td width="25%"><strong>Visited By </strong></td>
        <td width="35%"><strong>Referred from Page </strong></td>
      </tr>
      <?
      $queryP = "SELECT *,count(*) FROM stattracker WHERE ip =
    '127.0.0.1' GROUP BY page";
    $resultP=mysql_query($queryP);
    if($visitors>0){
    while ($rowP =mysql_fetch_assoc($resultP)) {
    //echo "Page: " .$row['page']. "Viewed: ".$row['count(*)']. "<br>";
    ?>
       <tr valign="top">
        <td><?=$rowP['page'];?></td>
        <td><?=$rowP['count(*)'];?></td>
        <td><?=$rowP['ip'];?></td>
        <td><?=$rowP['from_page'];?></td>
      </tr>
      <? }
      }else{
       ?>
      <tr><td><p>No Results</p></td></tr>
      <? }?>
    </table>
    </body>
    </html>

    What this entire code does is display the three main sets of information as we've discussed previously. It is only a very general snapshot of who visited what page when and what browser they used. You even get the total number of times a particular page was visited.

    Another area of analysis that I haven't discussed here is breaking the information down by the date of visit. You can for example run a query to find out the number of times a particular person visited in a month or year. There is just so much that you can do with the information that is collected by the script. There is no single best way of getting this information out, because it depends entirely on individual needs.

    Create two more PHP pages and call them p1.php and p2.php.  Include the 'theCollector.php' page in both of them. In the p1 one, add a link to p2 and in p2 add a link to p1. These two pages are going to be our test pages.

    Here's the code for p1.php. Use the exact same code when you create p2.php:

    Script: p1.php

    <?
    include "theCollector.php";
    ?>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-
    8859-1" />
    <title>Untitled Document</title>
    </head>
    <body>
    <p><a href="p1.php">page1</a></p>
    <p><a href="p2.php">page2</a></p>
    </body>
    </html>  

    Run p1.php, click on the link to p2.php and then click on the link to p1.php. Repeat this process a couple of times. This will populate the table with some data for us to use later on.

    Conclusion

    With the information that you get from the collector script, you can extract a lot more information than I did in this article. You can even use PHP's gd to give a graphical view of your web stats. But for most web masters, this should be adequate or act as a base to write more advanced web statistics scripts.



     
     
    >>> More PHP Articles          >>> More By Jacques Noah
     

       

    PHP ARTICLES

    - Implementing Factory Methods in PHP 5
    - Merging a File Split for FTP Upload using PHP
    - Getting Data from Yahoo Site Explorer Inboun...
    - Method Chaining: Adding More Selecting Metho...
    - How to Split a File During an FTP Upload Usi...
    - Expanding a Custom CodeIgniter Library with ...
    - Using the Yahoo Site Explorer Inbound Links ...
    - Building a CodeIgniter Custom Library with M...
    - Building an E-mini Trading System Using PHP ...
    - Completing the MySQL Class with Method Chain...
    - Building Dynamic Queries with Chainable Meth...
    - PHP Encryption and Decryption Methods
    - Building a MySQL Abstraction Class with Meth...
    - Completing a Sample String Processor with Me...
    - Mastering WHILE Loops for PHP and MySQL





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