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  
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 
Moblin 
JMSL Numerical Library 
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: 5 stars5 stars5 stars5 stars5 stars / 17
    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:
      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


    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


       · The whole problem of this kind of logging is often that it becomes tremendously slow...
       · You should add a disclaimer that your code is merely for educational/demonstrational...
       · in what way? plz explain...
       · There is no input validation or escaping going on.If I modified the HTTP_REFERER...
       · Wouldn't this add a lot of overhead to every page? Isn't it better to let the...
       · 1 database call doesn't actually add much overhead, but yea if you have access to...
       · that of course supposes a sound knowledge of the table structure ... which is mostly...
       · I disagree that a single db call does not add a lot of overhead. mostly it does not...
       · Why does index.php ask to include "trdbcon.php"? That was never talked about?
       · Think it should include the Database Information.
     

       

    PHP ARTICLES

    - Validating Web Forms with the Code Igniter P...
    - Output Buffering
    - Paginating Database Records with the Code Ig...
    - HTTP Headers in Web Development
    - Project Management: Administration
    - Building a Database-Driven Application with ...
    - User Authentication for a Project Management...
    - Introduction to the CodeIgniter PHP Framework
    - Adding Users for a Project Management Applic...
    - Migrating Class Code for a MIME Email to PHP...
    - Login and Logout Authentication for a Projec...
    - Composing Messages in HTML for MIME Email wi...
    - Project Management: Authentication
    - A Better Way to Determine MIME Types for MIM...
    - Project Management Overview





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