title: PHP error with a visitor counter using an image generator script
body: <p>I have made a short visitor counter script using a .txt file instead of a database. The stats are output through an image generator script. Both scripts work fine, but when they are combined instead of the numbers from the visitor counter going up by 1, they go up by 2 each page view. Would be greatly appreciated if anyone could take the time to see what i'm doing wrong. Have literally spent a few hours trying to figure it out and had no luck. </p>
  
  <p>First file (page_counter.html)</p>
  
  <pre><code>&lt;?php
  include 'config.php';
  /* -------------------------------------------------------------------------- */
  //                       HEX TO RGB CONVERTER CODE
  function hex2rgb($hex) {
    $hex = str_replace("#", "", $hex);
  
     if(strlen($hex) == 3) {
    $r = hexdec(substr($hex,0,1).substr($hex,0,1));
    $g = hexdec(substr($hex,1,1).substr($hex,1,1));
    $b = hexdec(substr($hex,2,1).substr($hex,2,1));
     } else {
    $r = hexdec(substr($hex,0,2));
    $g = hexdec(substr($hex,2,2));
    $b = hexdec(substr($hex,4,2));
     }
     $rgb = array($r, $g, $b);
  
    return $rgb; 
  }
  
  //hit counter code
  $counter = file_get_contents("count.txt");
  $counter = trim($counter);
  $counter += 1;
  file_put_contents("count.txt", $counter);
  
  //hex to rgb colour converter code
  $bg_rbg = hex2rgb(BACKGROUND_COLOUR_HEX);
  $txt_rbg = hex2rgb(TEXT_COLOUR_HEX);
  $shadow_rbg = hex2rgb(SHADOW_COLOUR_HEX);
  
  /* -------------------------------------------------------------------------- */
  //                       IMAGE GENERACTOR CODE
  
  header('Content-Type: image/png');
  
  //Creates the background
  $im = imagecreatetruecolor(IMAGE_WIDTH, IMAGE_HEIGHT);
  
  //Creates the colours
  $background = imagecolorallocate($im, $bg_rbg[0], $bg_rbg[1], $bg_rbg[2]);
  $main_text_colour = imagecolorallocate($im, $txt_rbg[0], $txt_rbg[1], $txt_rbg[2]);
  $shadow_colour = imagecolorallocate($im, $shadow_rbg[0], $shadow_rbg[1],                                                             $shadow_rbg[2]);
  imagefilledrectangle($im, 0, 0, 500, 100, $background);
  
  //Assigns a font path and font
  $font = dirname(__FILE__). '/' . FONT_TYPE;
  
  //Adds some shadow to the text
  imagettftext($im, FONT_SIZE, 0, MAIN_X+1, MAIN_Y+1, $shadow_colour, $font, $counter);
  
  //Creates the image
  imagettftext($im, FONT_SIZE, 0, MAIN_X, MAIN_Y, $main_text_colour, $font, $counter);
  
  //Using imagepng() results in clearer text compared with imagejpeg()
  imagepng($im);
  imagedestroy($im);
  ?&gt;
  </code></pre>
  
  <p>Second file (config.php)</p>
  
  <pre><code>&lt;?php
  //Enter your desired background image width below
  define('IMAGE_WIDTH', 90);
  
  //Enter your desired background image height below
  define('IMAGE_HEIGHT', 60);
  
  //Enter your desired background colour below
  define('BACKGROUND_COLOUR_HEX', '#FFFFFF');
  
  //Enter your desired text colour below
  define('TEXT_COLOUR_HEX', '#FF0000');
  
  //Enter your desired shadow colour below
  define('SHADOW_COLOUR_HEX', '#C0C0C0');
  
  //Enter a font size
  define('FONT_SIZE', 23);
  
  //Enter the X coordinates of the main text below
  define('MAIN_X', 20);
  
  //Enter the Y coordinates of the main text below
  define('MAIN_Y', 23);
  
  
  //Enter your desired font below
  define('FONT_TYPE', 'arial.ttf');
  ?&gt;
  </code></pre>
  
  <p>Third file is called count.txt and is where the visitor count is stored. Start it off with 0 or your desired number. </p>
  
  <p>Fourth file is just a font file, in my case arial.tff</p>