Mega Code Archive

 
Categories / Php / Cookie Session
 

Smart Counter - This little script is a plain and simple hit counter that

uses cookies to determine whether or not the visitor has already been counted. <?php $counterFile = "/home/httpd/html/counter/counter.text"; function displayCounter($counterFile) { global $counted $fp = fopen($counterFile,rw); $num = fgets($fp,5); if (!$counted) { $num += 1; unlink("$counterFile"); exec("echo $num > $counterFile"); } print "$num"; } if (!file_exists($counterFile)) { exec("echo 1 > $counterFile"); } displayCounter($counterFile); ?> /* Add the following to the very top of the page you want to count */ <?php if (!$counted) { setcookie("counted",1,time()+600); } ?>