Jump to content

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.