g_p_java Posted January 26, 2009 Share Posted January 26, 2009 Hello, i'm using the following code in order to count the guests in my website $guest = 0; $ip = $_SERVER["REMOTE_ADDR"]; $arr_guest = array("$guest" => $ip); // Store information in an array $guest++; // Increment $guest echo "Print array of guests .<br />"; $i = $guest; for ($i = 0 ; $i <= ($guest - 1); $i++) { echo " $i : $arr_guest[$i]" . "<br />"; } With the previous code, whenever someone visits my web page, the $guest becomes zero. I mean that $guest loses the previous value it has and starts from zero all the time! What shall i do in order to increment the counter? Thanks in advance! Quote Link to comment https://forums.phpfreaks.com/topic/142455-how-to-count-guests-in-a-dynamic-website/ Share on other sites More sharing options...
RichardRotterdam Posted January 26, 2009 Share Posted January 26, 2009 Because the variable will be reset each time the page gets viewed, you have to store the amount of times a page has been visited externally. This could be a in simple text file or a database. Quote Link to comment https://forums.phpfreaks.com/topic/142455-how-to-count-guests-in-a-dynamic-website/#findComment-746439 Share on other sites More sharing options...
g_p_java Posted January 26, 2009 Author Share Posted January 26, 2009 Because the variable will be reset each time the page gets viewed, you have to store the amount of times a page has been visited externally. This could be a in simple text file or a database. May you please post me a simple example, where someone stores the variable "$guest" in a text file, I mean are there specific functions that open a file and then write to it? Quote Link to comment https://forums.phpfreaks.com/topic/142455-how-to-count-guests-in-a-dynamic-website/#findComment-746600 Share on other sites More sharing options...
premiso Posted January 26, 2009 Share Posted January 26, 2009 fopen fread fwrite fclose In replace of fread there is file_get_contents or file There should be examples there but here is a basic example: <?php $file = "counter.txt"; $contents = file($file); $counter = $contents[0] + 1; $fh = fopen($file, "w"); fwrite($fh, $counter); fclose($fh); echo "The counter is now: " . $counter; ?> Quote Link to comment https://forums.phpfreaks.com/topic/142455-how-to-count-guests-in-a-dynamic-website/#findComment-746606 Share on other sites More sharing options...
g_p_java Posted January 26, 2009 Author Share Posted January 26, 2009 Thanks Quote Link to comment https://forums.phpfreaks.com/topic/142455-how-to-count-guests-in-a-dynamic-website/#findComment-746621 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.