Want to know who is visiting your website, when the peak times are, and how people are finding you? It is a lot easier than it sounds. While the code below may be over-simplified, it does get the job done. The code assumes that you have a include in your main source file that opens a database connection, and a code at the end of your source file that closes it.

The code will get the referring page, the date and time of access, and the IP of the users and store it in a table called tracker. You can then easily code a interface to view that database information. Later I will post code for an interface to this data.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
<?php
// very simple PHP function, call when database connection is open using tracker();
// By Steve Morrissey
 
function tracker()
{
	$referer = $_SERVER["HTTP_REFERER"];
	$time = date("F j, Y, g:i a");
	$ip = $_SERVER['REMOTE_ADDR'];
	if($referer == "")
	{
	     $referer = "Direct hit";
	}
	mysql_query("INSERT INTO tracker (id, referer, ip, accessed) VALUES('', '$referer', '$ip', '$time')") or die(mysql_error());
} // end tracker
?>