Jump to content

How to count guests in a dynamic website


g_p_java

Recommended Posts

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!

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?

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

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.