title: Counting the Number of Online Users Using jQuery Heartbeat (or PHP)
body: <p>I am currently using this php code to count the number of the users currently online:</p>
  
  <pre><code>$session_id = session_id();
  $time_stamp = time();
  $time_limit = $time_stamp - 300;        //   We give the session only 5 minutes if it exists
  $result = $this-&gt;select("SELECT * FROM `online_visitors` WHERE `session_id`='$session_id' LIMIT 1");
  if (!mysql_num_rows($result)) {
      $tb_col[0] = "visitor_ip";
      $tb_col[1] = "country";
      $tb_col[2] = "session_id";
      $tb_col[3] = "time_stamp";
      $tb_col[4] = "last_time_stamp";
      $tb_data[0] = "'" . $this-&gt;visitor_ip . "'";
      $tb_data[1] = "'" . $this-&gt;visitor_country . "'";
      $tb_data[2] = "'" . $session_id . "'";
      $tb_data[3] = "'" . $time_stamp . "'";
      $tb_data[4] = "'" . $time_stamp . "'";
      $this-&gt;insert("`online_visitors`", $tb_col, $tb_data);
  } else {
      $this-&gt;update("`online_visitors`", "`visitor_ip`='$this-&gt;visitor_ip', `country`='$this-&gt;visitor_country', `last_time_stamp`='$time_stamp'", "`session_id`='$session_id'");
  }
  
  $this-&gt;delete("`online_visitors`", "`last_time_stamp`&lt;'$time_limit'");
  </code></pre>
  
  <p>But it does not update in real time. I want to check the number of users every 30 seconds. I may also want to connect this table to the jQuery heartbeat function, so I know more reliably how many users are currently online.</p>
  
  <p>Am I approaching this properly? Any tips for how to achieve this in jQuery (I'm not very good with it)? Or can my approach be improved?</p>
  
  <p>I also want to improve my php code</p>