Jump to content

[SOLVED] quick question - visitor counter


skot

Recommended Posts

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

Link to comment
https://forums.phpfreaks.com/topic/108618-solved-quick-question-visitor-counter/
Share on other sites

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.

<?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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.