Page 1 of 1

cookie hit counter problems

Posted: Fri Feb 28, 2003 4:30 pm
by node
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 :D
Heres the code:

Code: Select all

<?

$c_ip = $_COOKIE&#1111;"user_ip"];
$c_visits = $_COOKIE&#1111;"visits"];
$counterFile = "counter.txt";
$ip = getenv("REMOTE_ADDR");
$visits = "0";


if ( isset($c_visits))&#123;
	$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;

&#125;

elseif (!($c_ip) || ($c_visits)) &#123; // 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);

&#125;
elseif ($c_ip != $ip)&#123; // 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);

&#125;

echo($hits);\n\n
echo($$c_visits);

?>
Thanks for the help...