Heres the code:
Code: Select all
<?
$c_ip = $_COOKIEї"user_ip"];
$c_visits = $_COOKIEї"visits"];
$counterFile = "counter.txt";
$ip = getenv("REMOTE_ADDR");
$visits = "0";
if ( isset($c_visits)){
$c_visits++; // add 1 to the cookie visit
Setcookie("visits", " ", time()-36000); // del old
Setcookie("visits", $c_visits, time()+36000); // create new
$$c_visits = $c_visits;
}
elseif (!($c_ip) || ($c_visits)) { // if no cookies add them, then add their hit
Setcookie("user_ip", $ip, time()+36000);
$visits++;
Setcookie("visits", $visits, time()+36000);
$fp = @fopen($counterFile, "r");
$hits = @fread($fp, filesize($counterFile));
fclose($fp);
$hits++;
$fp = @fopen($counterFile, "w");
fputs($fp, "$hits");
fclose($fp);
}
elseif ($c_ip != $ip){ // if IP is'nt the same as the cookie's var add their hit
Setcookie("user_ip", $ip, time()+36000);
$visits++;
Setcookie("visits", $visits, time()+36000);
$fp = @fopen($counterFile, "r");
$hits = @fread($fp, filesize($counterFile));
@fclose($fp);
$hits++;
$fp = fopen($counterFile, "w");
fputs($fp, "$hits");
fclose($fp);
}
echo($hits);\n\n
echo($$c_visits);
?>