The hit counter that I've installed increases by 2. Why?

        <?php
if(!file_exists("counter.txt"))
{$counter=fopen("counter.txt", "a" );}
else
{$counter=fopen("counter.txt","r+");}
$aufruf=fgets($counter,100);
$aufruf=$aufruf+1;
rewind($counter);
fputs($counter,$aufruf);
fclose($counter);
echo $aufruf;
?>

On the sever I have put a counter.txt file.

The homepage is www.aude-fleurs.ch

Any hints?

    How/where do you call the above code? Sounds like you're calling it twice per page load.

      The file is my index.php file. I don't know why it calls twice per page load.

        How large is your index.php file? Can you post all of it?

          bradgrafelman;10975589 wrote:

          How large is your index.php file? Can you post all of it?

          Thanks for looking into it. Here the code:

          <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
              "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
          <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
          <head>
          
          
          
          <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
          <meta name="description" content="Bienvenue chez Aude Fleurs et ses loisirs créatifs, Grand-Rue 10 1844 Villeneuve 021/960.47.60 ou 079/ 356.74.72 " />
          <meta name="keywords" content="Aude, Aude Nicaty, compositions florales, arrangement, Grand-Rue 10 1844 Villeneuve, floriste, livraison de fleurs, compositions florales, cours adultes, cours enfants" />
          <meta name="author" content="Paolo Portmann" />
          <meta name="page-type" content="Switzerland" />
          <meta name="page-topic" content="" />
          <meta name="robots" content="index, follow" />
          <meta name="rating" content="General" />
          <meta name="revisit-after" content="1 week" />
          <title>Bienvenue chez Aude Fleurs</title>
          <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
          <title>Bienvenue chez Audes Fleurs</title>
          
          <link rel="shortcut icon" type="image/x-icon" href="http://www.aude-fleurs.ch/favicon.ico">
          
          
          
          <style type="text/css">
          <!--
          body {
          	background-image: url();
          }
          .style1 {
          	font-size: 36px;
          	font-weight: bold;
          	color: #FFFF00;
          }
          a:hover {
          	color: #00CC00;
          }
          -->
          </style>
          </head>
          <body>
          <table width="900px" height="590px" align="center" cellpadding="0">
            <tr>
              <td background="acceuil_new.gif"><div align="center">
                <p>&nbsp;</p>
                <p>&nbsp;</p>
                <p>&nbsp;</p>
                <p>&nbsp;</p>
                <p>&nbsp;</p>
                <p><a href="prestations.html" class="style1">Bienvenus</a></p>
              </div></td>
            </tr>
          </table>
           <div align="center">
             <table width="12%" border="1">
               <tr class="style4"> 
                 <td align="center"> <font size="-1">
                   <?php
          if(!file_exists("counter.txt"))
          {$counter=fopen("counter.txt", "a" );}
          else
          {$counter=fopen("counter.txt","r+");}
          $aufruf=fgets($counter,100);
          $aufruf=$aufruf+1;
          rewind($counter);
          fputs($counter,$aufruf);
          fclose($counter);
          echo $aufruf;
          ?>
                   </font><span class="style4">visiteurs </span></td>
                    </tr>
             </table>
             </div>
          
          <table align="center">
          		<tr>
                    <td align="center"><object
          data="xspf_player_slim.swf?playlist_url=musique.xspf&autoplay=true" type="application/x-shockwave-flash" width="200" height="20" align="absmiddle">
                      <param name="movie" value="xspf_player_slim.swf?playlist_url=musique.xspf&autoplay=true" />
                    </object></td>
                  </tr>
          </table>
          
          <table width="900px" align="center" cellspacing="0" cellpadding="0">
          <tr>
            <td><div align="center">Paolo Portmann &copy; 2010 - 2011,
              derni&egrave;re mise &agrave; jour de cette page le
          <!-- #BeginDate format:Ge1 -->01.03.2011<!-- #EndDate -->
                </div></td>
                </tr>
            </table>
          </body>
          </html>

          Also without the xspf player and the "<!-- #BeginDate format:Ge1 -->03.03.2011<!-- #EndDate -->" it counts 2 visitors per load.

          Thank you

            5 days later

            hmm... There is an easier way you could go about this.

            Create a file, call it count.php
            Enter the following source, I recommend studying and asking about it so that its useful in the future:

            $ecount = ("hits.txt");
            $hits = file($ecount);
            $count = intval(trim(file_get_contents($ecount)));
            $count = $count + 1;
            $fp = fopen($ecount , "w");
            fputs($fp , "$count");
            fclose($fp);
            echo $count;
            

            Now create a file called hits.txt, inside it write 0.

            Now, on your index page, call the counter by using either code.
            If your index ends in .php:

            include('count.php');
            

            Although if it ends in anything else:

            <table><tr><td>Hits: <b>
            <?php
            include('count.php);
            ?>
            </b></td></tr></table>
            

            Best of luck!

              I've found the error. This was my style sheet:

              <style type="text/css">
              <!--
              body {
              background-image: url();
              }
              .style1 {
              font-size: 36px;
              font-weight: bold;
              color: #FFFF00;
              }
              a:hover {
              color: #00CC00;
              }
              -->

              If I remove

              body {
              background-image: url();

              from the style sheet, the counter counts correctly. I can not explain why.

              Thanks to all of you.

                paldo wrote:

                I can not explain why.

                I believe I can. Specifying 'url();' makes the browser re-request the same URL and parse it as a background image. It fails, of course, but the request was counted by your hit counter.

                  Write a Reply...