<?php
/*************************************************************************
*                                                                        *
* This script counts the hits and unique visitors                        *
*                                                                        *
* Copyright (C) 2004-2005 PatheticCockroach <http://www.mpam3.fr.st>     *
*                                               Revision #1              *
*                                                                        *
* This program is free software; you can redistribute it and/or modify   *
* it under the terms of the GNU General Public License as published by   *
* the Free Software Foundation; either version 2, or (at your option)    *
* any later version.                                                     *
*                                                                        *
* This program is distributed in the hope that it will be useful,        *
* but WITHOUT ANY WARRANTY; without even the implied warranty of         *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the          *
* GNU General Public License for more details.                           *
*                                                                        *
* You should have received a copy of the GNU General Public License      *
* along with this program; if not, write to the Free Software            *
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.              *
* You can also provide yourself with an online copy of the text:         *
* <http://www.gnu.org/copyleft/gpl.html>                                 *
* <http://www.gnu.org/copyleft/gpl.txt>                                  *
*                                                                        *
*************************************************************************/

$db_host="127.0.0.1";	// PARAMETERS OF THE DB
$db_user="username";
$db_pass="password";
$database="database_name";

mysql_connect($db_host,$db_user,$db_pass) or die("Unable to connect to database");
mysql_select_db($database) or die( "Unable to select database");

$tps_unique=3600*24;		// Enter here the time, in seconds, during which an IP is considered to be the same unique visitor
$cfile="count.txt";		// Enter here the name of the file where the script will store its data (hits and unique visitors count)

/*	This part of the code is optional, it enables to lighten some of the code after, but I'm not sure wether it's a good choice (it adds a MySQL query for each hit...), so I just let it as an "option"
$date_limite=time()-$tps_unique;			// We delete "outdated visitors"
mysql_query("DELETE FROM iplog WHERE heure <$date_limite");
*/

$now=time();
$IP=$_SERVER['REMOTE_ADDR'];
$query = "SELECT * FROM iplog WHERE IP='$IP'";
$resultat = mysql_query($query);
if($resultat==false)die('Error : can&#39;t find data table.');
$unique=true;
if(false!=($ligne = mysql_fetch_array ($resultat)))	// If the IP exists in the table... we'll check how long it's been since we last saw it
	{
	$heure_limite=$ligne['heure']+$tps_unique;
	if($now<$heure_limite)				// If the IP has been added for a shorter time than $tps_unique, it's not a new visitor
		{$unique=false;}			// Note : the script considers a visitor as unique when he comes back over $tps_unique after their first visit, if you want to consider his latest visit instead of the first one, you should add mysql_query("UPDATE iplog SET heure='$now' WHERE IP ='$IP'"); here (or - better - remove the else, { and } if you know what I mean)
		else					// If the IP has been added for a longer time than $tps_unique, it's a new visitor (so $unique=true) and we update the table
		{mysql_query("UPDATE iplog SET heure='$now' WHERE IP ='$IP'");}
	}
else
	{
	mysql_query("INSERT INTO iplog VALUES ('','$IP','$now')");
	}
mysql_close();

// ************ This part of the code handles a missing $cfile error, you can remove it if you're sure you won't delete the $cfile file
if(!file_exists($cfile))		// If $cfile doesn't exists, we create it with 0 values
	{
	$fp=fopen($cfile,"w");
	fputs($fp, "0|0");
	fclose($fp);
	}
// ************ fin de gestion de l'erreur

$fp=fopen($cfile,"r");			// We open the file and read the 2 values
$inp = fread($fp, filesize($cfile));
$tmpvar = explode('|', $inp);
fclose($fp);

$tmpvar[0]++;				// We add a hit
if($unique!=false){$tmpvar[1]++;}	// We add a visitor if needed

$fp=fopen($cfile,"w");			// We put the new data in the file
fputs($fp, $tmpvar[0]."|".$tmpvar[1]);
fclose($fp);

echo $tmpvar[0]. ' hits and ' .$tmpvar[1]. ' visitors.';		// Shows hits and visitors, to be edited for a better look :)
									// You could also add a function to put the plural only when needed... but I don't think it's often useful :)
?>