login
.net

PHPCodeSnipCounter

login
down

a simple counter with IP blocking

The following code counts the visitors to a page (or the page its included in) saving them into a file. It blocks double visits checking the IP, and doesn't count my visits when I look for my page. This is done by comparing the visitors IP with my resolved Dyndns address.

<?php
#saves the counter value and the last IPs in the here defined file:
#config
define("COUNTER_FILE", "count.dat"); //make sure its writeable!
define("IP_SPERRE", 3600*12); //block double visits this time
define("MAX_LINES", 100); //max number of IPs in the file
#script
$filecont=file(COUNTER_FILE);
$visits=intval(array_shift($filecont));
$rsoft_ip=gethostbyname("my.dyndns.org"); //dont count myself
$visitor_ip=$_SERVER['REMOTE_ADDR'];
$ip_already_counted=0;
foreach($filecont as $line){
        $line=str_replace("n", "", $line);
        list($ip, $time)=explode("t", $line);
        if($ip==$visitor_ip && time()-$time<IP_SPERRE){
                $ip_already_counted=1;
        }
}
if(!$ip_already_counted && $visitor_ip != $rsoft_ip){
        $visits++;
        $fh=fopen(COUNTER_FILE, "w");
        @fwrite($fh, $visits."n");
        @fwrite($fh, $visitor_ip."t".time()."n");
        for($i=0;$i<count($filecont) && $i<MAX_LINES;$i++){
                @fwrite($fh, str_replace("n", "", $filecont[$i])."n");
        }
        @fclose($fh);
}
echo $visits; //do what you want with the countvalue here
 
?>
 
 

up
Home upload edit Recent Changes Find Page All Pages