KashMoney Posted December 4, 2007 Share Posted December 4, 2007 Hello, I am trying to do a hit counter that writes an ip address to a text file. I only want ip's that are unique. This is what I have; <?php $filename = "hits.txt"; $file = file($filename); $file = array_unique($file); $hits = count($file); echo $hits; $fd = fopen ($filename , "r"); $fstring = fread ($fd , filesize ($filename)); fclose($fd); $fd = fopen ($filename , "w"); $fcounted = $fstring."\n".getenv("REMOTE_ADDR"); $fout= fwrite ($fd , $fcounted ); fclose($fd); ?> Then ovbiously the hits.txt logs all of the hit counter unique ip address. The problem I have with this script is that every time I reload the number says at 3 it does add +1 every time. Any Help on that? Thanks, KashMoney Quote Link to comment Share on other sites More sharing options...
PHP_PhREEEk Posted December 4, 2007 Share Posted December 4, 2007 Are you saying that count($file) is off by 1? If so, print_r($file) to screen and see where the problem is. There might be an empty array element in there, or some other garbage. PhREEEk Quote Link to comment Share on other sites More sharing options...
rajivgonsalves Posted December 4, 2007 Share Posted December 4, 2007 I think your writing to the file after reading from it you should write to the file first and then read from it <?php $filename = "hits.txt"; $fd = fopen ($filename , "r"); $fstring = fread ($fd , filesize ($filename)); fclose($fd); $fd = fopen ($filename , "w"); $fcounted = $fstring."\n".getenv("REMOTE_ADDR"); $fout= fwrite ($fd , $fcounted ); fclose($fd); $file = file($filename); $file = array_unique($file); $hits = count($file); echo $hits; ?> Quote Link to comment Share on other sites More sharing options...
KashMoney Posted December 4, 2007 Author Share Posted December 4, 2007 I tried your method rajivgonsalves but its saying at three for some reason. So weird! At first when i reset my browser I get this mesage Warning: fread() [function.fread]: Length parameter must be greater than 0. in /home/mmaaaco/public_html/kkashou/homework/q3/counter.php on line 6 2. After I refresh again its goes back to 3. Hope this helps. Anything else? KashMoney Quote Link to comment Share on other sites More sharing options...
rajivgonsalves Posted December 4, 2007 Share Posted December 4, 2007 to avoid that error your code should be <?php $filename = "hits.txt"; $fd = fopen ($filename , "a+"); fwrite ($fd , getenv("REMOTE_ADDR")."\n" ); fclose($fd); $file = file($filename); $file = array_unique($file); $hits = count($file); echo $hits; ?> Quote Link to comment Share on other sites More sharing options...
KashMoney Posted December 4, 2007 Author Share Posted December 4, 2007 Well the error doesn't show up anymore which is a good thing but now the number is at 1 it does change after a refresh or when i go back and click on that page! Quote Link to comment Share on other sites More sharing options...
rajivgonsalves Posted December 4, 2007 Share Posted December 4, 2007 see your hits.txt file the ip will be unique so therefore it will always show as one try accessing the page from another pc Quote Link to comment Share on other sites More sharing options...
KashMoney Posted December 4, 2007 Author Share Posted December 4, 2007 Thank you very much! KashMoney Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.