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.

#saves the counter value and the last IPs in the here defined file:
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
$rsoft_ip=gethostbyname("my.dyndns.org"); //dont count myself
foreach($filecont as $line){
        $line=str_replace("n", "", $line);
        list($ip, $time)=explode("t", $line);
        if($ip==$visitor_ip && time()-$time<IP_SPERRE){
if(!$ip_already_counted && $visitor_ip != $rsoft_ip){
        $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");
echo $visits; //do what you want with the countvalue here

Home upload edit Recent Changes Find Page All Pages