title: Hit counter for each page with PHP
body: <p>I want to integrate a counter on my webpage, as far I've done it so the counter works. But what I really want is this counter to be specific for each page to be generated per <code>id</code></p>
  
  <pre><code>&lt;?php
  
  session_start();
  
  $counter_name = 'counter'.$id.'.txt';
  // Check if a text file exists. If not create one and initialize it to zero.
  if (!file_exists($counter_name)) {
    $f = fopen($counter_name, "w");
    fwrite($f,"0");
    fclose($f);
  }
  // Read the current value of our counter file
  $f = fopen($counter_name,"r");
  $counterVal = fread($f, filesize($counter_name));
  fclose($f);
  // Has visitor been counted in this session?
  // If not, increase counter value by one
  if(!isset($_SESSION['hasVisited'])){
    $_SESSION['hasVisited']="no";
    $counterVal++;
    $f = fopen($counter_name, "w");
    fwrite($f, $counterVal);
    fclose($f); 
  }
  else {
      $counterVal++;
      $f = fopen($counter_name, "w");
      fwrite($f, $counterVal);
      fclose($f);
  }
  $counterVal = str_pad($counterVal, 5, "0", STR_PAD_LEFT);
  $chars = preg_split('//', $counterVal);
  $im = imagecreatefrompng("canvas.png");
  $src1 = imagecreatefrompng("$chars[1].png");
  $src2 = imagecreatefrompng("$chars[2].png");
  $src3 = imagecreatefrompng("$chars[3].png");
  $src4 = imagecreatefrompng("$chars[4].png");
  $src5 = imagecreatefrompng("$chars[5].png");
  imagecopymerge($im, $src1, 0, 0, 0, 0, 15, 15, 100);
  imagecopymerge($im, $src2, 15, 0, 0, 0, 15, 15, 100);
  imagecopymerge($im, $src3, 30, 0, 0, 0, 15, 15, 100);
  imagecopymerge($im, $src4, 45, 0, 0, 0, 15, 15, 100);
  imagecopymerge($im, $src5, 60, 0, 0, 0, 15, 15, 100);
  // Output and free from memory
  header('Content-Type: image/png');
  echo imagepng($im);
  imagedestroy($im);
  ?&gt;
  </code></pre>
  
  <p>With this code I can see only the hits for the main page. It shows on each page with the same number 
  ex.</p>
  
  <pre><code>page 1 count: 100 
  page 2 count: 100
  </code></pre>
  
  <p>after I refresh 50x page 1</p>
  
  <pre><code>page 1 count: 150
  page 2 count: 150
  </code></pre>
  
  <p>The file name is <code>counter.php</code>and implemented on the template file with the following code</p>
  
  <pre><code> &lt;img style="padding-left: 2px;" alt="Hit counter" src="http://www.example.com/counter/counter.php" /&gt;
  </code></pre>
  
  <p>I've been searching for a solution but till now haven't found any.<br>Thanks in Advance.</p>
  
  <p>Best Regards,<br>
  BujarA.</p>