Access Counter

Guys
I want to put on the website that I developed an access counter
But the way I did is counting every time you open a page
I want to count only when the index page is accessed
This is because the counter is in the menu that is included on all pages.
So it’s actually counting how many times the menu was displayed… this is wrong
I thought about making a javascript function so that a variable is incremented each time the page “index.php” was displayed and then just print the variable in the menu, but I do not know how to do this
Can anyone help me?

This is the site

You maybe able to wrapper the code that increments the figure in an ‘if’ statement like so.


<?php
if('index.php' === basename($_SERVER['PHP_SELF'])){
  #increment counter
}

I had to take care of other things, but I’m back!
And I tried this, but it did not work.
Any other suggestions, please?

This is how i’m doing…


<? 
        $counter = "visitas.txt";
        $fd = fopen($counter, "r"); 
        $num =  fread($fd, filesize( $counter )); 
        fclose($fd); 
        $fd = fopen($counter, "w"); 
        $users = $num + 1; 
        echo "$users"; 
        fwrite($fd, $users); 
        fclose($fd); 
?> 

If I put a condition does not write the number of hits


<? 
        $counter = "visitas.txt";
        $fd = fopen($counter, "r"); 
        $num =  fread($fd, filesize( $counter )); 
        fclose($fd); 
        $fd = fopen($counter, "w");
        if('index.php' === basename($_SERVER['PHP_SELF'])) {
               $users = $num + 1; 
        }
        echo "$users"; 
        fwrite($fd, $users); 
        fclose($fd); 
?> 

Well lets get some things in the right places, fix a tag, and not use an extra variable


<?[COLOR="Green"]php[/COLOR] 
[COLOR="Blue"]if('index.php' === basename($_SERVER['PHP_SELF'])) {[/COLOR]
        $counter = "visitas.txt";
        $fd = fopen($counter, "r"); 
        $num =  fread($fd, filesize( $counter )); 
        fclose($fd); 
        $fd = fopen($counter, "w");
[COLOR="Orange"]        $num++; [/COLOR]
        fwrite($fd, [COLOR="orange"]$num[/COLOR]); 
        fclose($fd); 
        echo [COLOR="orange"]$num; [/COLOR]
[COLOR="Blue"]}[/COLOR]
?>

Like this does not appear the number of hits on other pages, only appears on the index.
And it’s counting every two.

Like this also does not work:


<?php
if('index.php' === basename($_SERVER['PHP_SELF'])) {
        $counter = "visitas.txt";
        $fd = fopen($counter, "r"); 
        $num =  fread($fd, filesize( $counter )); 
        fclose($fd); 
        $fd = fopen($counter, "w");
        $num++;
        fwrite($fd, $num); 
        fclose($fd); 
        echo $num;
}
else echo $num;
?>

Okay so you want the echo on all pages… then… one more move


<?php 
$counter = "visitas.txt";
$fd = fopen($counter, "r"); 
$num =  fread($fd, filesize( $counter )); 
fclose($fd); 
[COLOR="blue"]if('index.php' === basename($_SERVER['PHP_SELF'])) {[/COLOR]
        $fd = fopen($counter, "w");
        $num++; 
        fwrite($fd, $num); 
        fclose($fd); 
[COLOR="Blue"]}[/COLOR]
echo $num; 
?>

It work! Thanks!