PHP page-view counter

software development


Hi. I am calling a small PHP document on each page of my website to keep track of pageviews, while excluding any views that are made from my specific IP addreass. The system I developed works perfect and even creates the .txt document in the corresponding directory if the file doesn’t exist.

My problem: Sometimes when I get to a high number of page views, my counter will reset back to zero. I was wondering if this is because of my programming or maybe from user input. I tried to eliminate all user input on my website, however the problem still occurs.

Here is the short code that I am using for a page counter. It gets the IP of the viewer, and, if it isn’t my IP address, it will access or create ‘hitcounter.txt’ and increment the recorded data within ‘hitcounter.txt’ by one.

<?php function getIP() { $ip; if (getenv("HTTP_CLIENT_IP")) $ip = getenv("HTTP_CLIENT_IP"); else if(getenv("HTTP_X_FORWARDED_FOR")) $ip = getenv("HTTP_X_FORWARDED_FOR"); else if(getenv("REMOTE_ADDR")) $ip = getenv("REMOTE_ADDR"); else $ip = "UNKNOWN"; return $ip; } if (getIP() != "[color=#808000]ENTER IP ADDRESS[/color]") { ini_set('memory_limit', '5M'); $count_my_page = ("hitcounter.txt"); $hits = file($count_my_page); $hits[0] ++; $fp = fopen($count_my_page , "w"); fputs($fp , "$hits[0]"); fclose($fp); echo $hits[0]; } else{ echo "Hi Izak
"; $count_my_page = ("hitcounter.txt"); $hits = file($count_my_page); $fp = fopen($count_my_page , "w"); fputs($fp , "$hits[0]"); fclose($fp); echo $hits[0]; } ?>

So is there some sort of limit that it will count up to? I have been struggling to keep my pageviews going after ~20,000 views. Then it will start to reset back to zero sometimes. However I can manually set the views back to ~20,000 and it seems like every thousand views it resets. Please let me know if you have a suggestion or question; Thanks!