cookie hit counter problems
Posted: Fri Feb 28, 2003 4:30 pm
well what im trying to do is, make a somewhat unique hit counter using cookies. One cookie holds their IP and the other how many times they have visited. For some reason it just keeps counting no even though the cookie was set. The cooke for counting the visits gets set, but when i try and display the var `$$c_visit` to show how many times they have been it doesnt show anything. I am still very much new to PHP, just trying to get better
Heres the code:
Thanks for the help...
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);
?>