title: The best way or code to create counter hits using session, PHP and MySQL
body: <p>Below, I have pasted the code that I found the best way to do counter hits using PHP, MySQL and session. But I want to find out is there any other way to do this process? Or this way is only the way to do counter hits on session? I want to learn other ways as well, if you guys know it then it will be really helpful. </p>
  
  <pre><code>&lt;?php
  session_start();
  include_once"db.inc.php";
  $webpage=htmlspecialchars($_SERVER["REQUEST_URI"]);
  
  $sql= "CREATE TABLE IF NOT EXISTS counter (
  id int(4) NOT NULL auto_increment,
  webpage varchar(90) NOT NULL,
  visitors int(11) NOT NULL default '1',
  PRIMARY KEY (id)
  ) ENGINE=MyISAM AUTO_INCREMENT=1"; 
  
  mysql_query($sql);
  
  $result=mysql_query("SELECT * FROM counter WHERE webpage='$webpage'");
  $num_rows= mysql_num_rows($result);
  if ($num_rows== 0){
  mysql_query("INSERT INTO counter (id, webpage, visitors)
  VALUES ('','$webpage','1')");
  
  }else{
  
  if (!isset($_SESSION['webpage'])){$_SESSION['webpage']= 0;
  mysql_query("UPDATE counter SET visitors=visitors+'1' WHERE webpage='$webpage'");}}
  ?&gt;
  
  &lt;html&gt; 
  &lt;head&gt; 
  &lt;meta http-equiv="Content-Type" content="text/html; charset=utf-8" /&gt;
  &lt;title&gt; Just a test &lt;/title&gt;
  
  &lt;/head&gt;
  &lt;body&gt; 
  
  &lt;?php
  $result=mysql_query("SELECT * FROM counter WHERE webpage='$webpage'");
  $row=mysql_fetch_assoc($result);
  $n=$row['visitors'];
  $l=9-strlen($n);
  $z=substr('',0,$l);
  $c="Counter: ".$z.$n;
  
  echo "&lt;div class='counter'&gt;".$c."&lt;/div&gt;";
  
  mysql_close();
  //session_unset();
  //session_destroy();
  
  ?&gt;
  &lt;/body&gt;
  &lt;/html&gt; 
  </code></pre>