PHP
  Home arrow PHP arrow 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? 
PHP

Tracking Website Statistics with PHP
By: Jacques Noah
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 19
    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
    ( Page 1 of 4 )

    If you have a website, especially one that sells products or services, it can be useful to track the visitors to your site. This article explains how to write a program that will do just that.

    Introduction

    In this article we are going to discuss how to create a program that will track the visitors to your site. We are going to do this by:

    • Recording the visitors' IP address.
    • Recording the time and date of the visit.
    • Recording the pages the visitor viewed.
    • Recording the name of the browser the visitor is using.
    • Recording the page the person came from.

    All this will be recorded for every page on a website, simply by including the code that we are going to create in a moment.

    All this information is going to give us the ability to know which pages are viewed the most and which ones the least. It will also let us know what pages a particular visitor views the most, by means of the visitor's IP address.

    The Database Table

    Create a database and give it whatever name you like. We now need to create a table. We need the following data:

    IP address -Visitor's IP address

    Date_visited

    Page- Page visited

    Browser-Name of the browser

    frompage- Page from which the person came

    With the above in mind, let's create the table:

    CREATE TABLE `statTracker` (
      `id` int(11) NOT NULL auto_increment,
      `browser` varchar(255) NOT NULL default '',
      `ip` varchar(15) NOT NULL default '',
      `thedate_visited` date NOT NULL default '0000-00-00',
      `page` varchar(70) NOT NULL default '',
      `from_page` varchar(200) NOT NULL default '',
      PRIMARY KEY  (`id`)
    ) TYPE=MyISAM AUTO_INCREMENT=8 ;

    The table should be very easy for you to understand. Now, without further ado, let's create the core tracking script. This is the script that you should attach to every page on your site:

    Script: theCollector.php

    <?
    //collect information...
    $browser  =$_SERVER['HTTP_USER_AGENT']; // get the browser name
    $curr_page=$_SERVER['PHP_SELF'];// get page name
    $ip  =  $_SERVER['REMOTE_ADDR'];   // get the IP address
    $from_page = $_SERVER['HTTP_REFERER'];//  page from which visitor
    came
    $page=$_SERVER['PHP_SELF'];//get current page
    //Insert the data in the table...
    $query_insert  ="INSERT INTO stattracker
    (browser,ip,date_visited,page,from_page) VALUES
    ('$browser','$ip',now(),'$page','$from_page')" ;
    $result=mysql_query ( $query_insert);
    if(!$result){
    die(mysql_error());
    }

    //remove this section when attaching the script to a webpage
    //I include it only because of it's debug value.
    $query="Select Count(*) from stattracker WHERE page = '$curr_page'";
    $result=mysql_query($query);
    $viewed = mysql_result($result,0,'count(*)');
    echo "This page was viewed $viewed times";
    ?>

    This script includes two sections, one to collect the data we require, and one to insert the collected data into the database.

    There are only a few points about the code worth explaining. The "$_SERVER[]" is a server array that contains a lot of useful variables. You can determine the host name, remote port or even the scriptname.

    The Count(*) retrieves all the rows in the table that meets the condition. In the  "$viewed = mysql_result($result,0,'count(*)')" line the "0" refers to the row; at this point there is no row in the table since we have not run the script yet.



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

       

    PHP ARTICLES

    - Using Directory Iterators to Build Loader Ap...
    - Using the spl_autoload() Functions to Build ...
    - Working Out of the Object Context to Build L...
    - Using the _autoload() Magic Function to Buil...
    - The Destruct Magic Function in PHP 5
    - The Autoload Magic Function in PHP 5
    - Developing a Recursive Loading Class for Loa...
    - The Sleep and Wakeup Magic Functions in PHP 5
    - Using the Clone Magic Function in PHP 5
    - Including Files Recursively with Loader Appl...
    - The Call Magic Function in PHP 5
    - Designing a Captcha System with PHP and MySQL
    - Using Static Methods to Build Loader Apps in...
    - The Isset and Unset Magic Functions in PHP 5
    - Advanced PHP Form Input Validation to Check ...





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