Mega Code Archive

 
Categories / Php / File Directory
 

OK guys, this script is a simple file based counter

This is meant for sites that do not run a db and instead, use a file and a cookie. It isn't the best I've seen but it's simple. Consider this as a tutorial. :) Just create a file called counter.txt and put this where you want to display the counter. Remember to set the permissions on counter.txt so that your webserver can write to the file. <? $visitor_ip = $HTTP_COOKIE_VARS["user_ip"]; $counter = "counter.txt"; $counter_file_line = file($counter); if(!$vistor_ip) { setcookie("user_ip", $REMOTE_ADDR, time()+360000); $counter_file_line[0]++; $cf = fopen($counter, "w+"); fputs($cf, "$counter_file_line[0]"); fclose($cf); } elseif($vistor_ip != $REMOTE_ADDR) { $counter_file_line[0]++; $cf = fopen($counter, "w+"); fputs($cf, "$counter_file_line[0]"); fclose($cf); } ?>