View Single Post
Old 11-08-2005, 02:41 PM   #2 (permalink)
tgo
Registered User
 
Join Date: Jul 2005
Posts: 185
OS: slackware 10.1


Send a message via AIM to tgo
I dont think apache holds referers in logs except for on some errors. But here is a php script I wrote that count unique hits that you can use.

Code:
<?php
// get name of current page
$script = $_SERVER['PHP_SELF'];

// break the array in directories and the script name
$all = explode('/',$script);

//get name of page
$spot = count($all) -1;
$script = $all[$spot];
$ip = $_SERVER['REMOTE_ADDR'];

// query to check for whether the page is in the table or not
$link = "SELECT * FROM hits WHERE script='$script'";
$res = mysql_query($link) or die(mysql_error());
$total = mysql_num_rows($res);
$row = mysql_fetch_assoc($res);

// if its not then add it with one hit
if ($total == 0)
{
	$add = "INSERT INTO hits VALUES('$script','1','$ip')";
	$addit = mysql_query($add) or die(mysql_error());
}
else
{
        // if it is and its not from an ip thats already hit it then add it	
	$ips = $row['ips'];
	if (!strstr($ips,$ip))
	{
		$newtotal = $row['total'] + 1;
		$ips .= $ip;
		$update = mysql_query("UPDATE hits SET total='$newtotal',ips='$ips' WHERE script='$script'") or die(mysql_error());
	}
}
?>
And the code to make the table:

Code:
<?php
 $stats = "CREATE TABLE `hits` (
    `script` TEXT NOT NULL,
    `total` INT ( 11 ),
    `ips` TEXT NOT NULL );";
    $table = mysql_query($stats) or die(mysql_error());
?>
To use it just create the table and name the file stats.php. Then whatever pages you want tracked add

<?php include("stats.php"); ?>
__________________
My new homepage:

Last edited by tgo : 11-08-2005 at 03:11 PM.
tgo is offline   Reply With Quote