PHP Counter --
Session Control
After you set up the counter, you may find that counter
will update whenever you refresh the page. That will lead incorrect
hit counter.
Here is the fix of the above problems. We use session
variable to keep track of each session which is browsing your page.
One session counts as one hit.
In order to do this, we need add the following codes to
graphcount.php.
Open graphcount.php in your favorite editor.
At the second line, right under <?php, add session_start();
Then find:
// If the log file exist lets read count from it
else {
$count = @fread($file, filesize($logfile)) or $count=0;
fclose($file);
// Raise the value of $count by 1
$count++;
Change to
// If the log file exist lets read count from
it
else {
$count = @fread($file, filesize($logfile)) or $count=0;
fclose($file);
// Raise the value of $count by 1
if (!isset($_SESSION['count']))
{ $_SESSION['count'] = 0;
$count++;}
See the code in red. It checks the existence of session variable
"count" , if it exists, it means this session
is already taken into account. Otherwise add 1 more to our counter.
This tutorial includes the following section:
|