skot Posted June 4, 2008 Share Posted June 4, 2008 Hi In theory I saw this small bit of code working, however when set to the test value of 100, the first time I tested this the value of 101 was written to the file and displayed but any subsequent refreshes do not cause the counter to increment. $file = "mcount.txt"; if (!empty($file)) { $file = file_get_contents("$file"); $count = ($file + 1); $fp = fopen($file, "w"); fwrite($fp, $count); fclose($fp); } echo "<p class=\"box2\" align=\"center\">Visitor count: <i>" . $count . "</i>.</font></p><br>\n"; Any ideas why? Page at www.bridgey.net/music Quote Link to comment https://forums.phpfreaks.com/topic/108618-solved-quick-question-visitor-counter/ Share on other sites More sharing options...
Wolphie Posted June 4, 2008 Share Posted June 4, 2008 One issue could be concerning the quotes around the file_get_contents() parameter. I'm not too sure though, I haven't used files for storage in a long time, I usually use a database. However, this method isn't strictly reliable, since every page refresh would increase the counter. It should rely on storing unique IP addresses. From my own experience, reading a files contents, opening a file and then writing to a file may not seem like a large load on the server in small amounts, but if people were to keep refreshing, or create a macro to refresh the page all the time, the load will increase dramatically. Quote Link to comment https://forums.phpfreaks.com/topic/108618-solved-quick-question-visitor-counter/#findComment-557080 Share on other sites More sharing options...
.josh Posted June 4, 2008 Share Posted June 4, 2008 <?php if ($file = file_get_contents("mcount.txt")) { $file++; file_put_contents("mcount.txt",$file); } echo "<p class=\"box2\" align=\"center\">Visitor count: <i>" . $file . "</i>.</font></p><br>\n"; ?> However, I would take Wolphie's advice about the whole unique visitor thing. Quote Link to comment https://forums.phpfreaks.com/topic/108618-solved-quick-question-visitor-counter/#findComment-557090 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.