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:
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
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` (
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:
//remove this section when attaching the script to a webpage
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.
blog comments powered by Disqus