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.