Question related to count the guests in a website

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
g_p
Forum Newbie
Posts: 13
Joined: Sun Jan 25, 2009 9:49 am

Question related to count the guests in a website

Post by g_p »

Hello, i'm using the following code in order to count the guests in my website

Code: Select all

 
$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 keep the value of $guest?

Thanks in advance! :)
Mark Baker
Forum Regular
Posts: 710
Joined: Thu Oct 30, 2008 6:24 pm

Re: Question related to count the guests in a website

Post by Mark Baker »

g_p wrote:Hello, i'm using the following code in order to count the guests in my website
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 keep the value of $guest?
It does. That's the way it works.
If you want to retain your list of guests, then you have to "persist" the array, eithr storing the information in a database, or a file, or even shared memory (if you're using APC or similar)
g_p
Forum Newbie
Posts: 13
Joined: Sun Jan 25, 2009 9:49 am

Re: Question related to count the guests in a website

Post by g_p »

Mark Baker wrote:
g_p wrote:Hello, i'm using the following code in order to count the guests in my website
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 keep the value of $guest?
It does. That's the way it works.
If you want to retain your list of guests, then you have to "persist" the array, eithr storing the information in a database, or a file, or even shared memory (if you're using APC or similar)
Hi, thanks for replying, When you say to store it in a file, like txt?
I mean does php support this? I may store information from php to a simple file?
Also, i searched google for APC but i didn't understand what it is, may you please tell me?
May you please post me a very simple example of php where sm writes something from php to a file?

thanks in advance! :)
g_p
Forum Newbie
Posts: 13
Joined: Sun Jan 25, 2009 9:49 am

Re: Question related to count the guests in a website

Post by g_p »

User avatar
papa
Forum Regular
Posts: 958
Joined: Wed Aug 27, 2008 3:36 am
Location: Sweden/Sthlm

Re: Question related to count the guests in a website

Post by papa »

True story! :)
Post Reply