0

I am currently using this php code to count the number of the users currently online:

$session_id = session_id();
$time_stamp = time();
$time_limit = $time_stamp - 300;        //   We give the session only 5 minutes if it exists
$result = $this->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->visitor_ip . "'";
    $tb_data[1] = "'" . $this->visitor_country . "'";
    $tb_data[2] = "'" . $session_id . "'";
    $tb_data[3] = "'" . $time_stamp . "'";
    $tb_data[4] = "'" . $time_stamp . "'";
    $this->insert("`online_visitors`", $tb_col, $tb_data);
} else {
    $this->update("`online_visitors`", "`visitor_ip`='$this->visitor_ip', `country`='$this->visitor_country', `last_time_stamp`='$time_stamp'", "`session_id`='$session_id'");
}

$this->delete("`online_visitors`", "`last_time_stamp`<'$time_limit'");

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.

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?

I also want to improve my php code

  • Your best bet is the php equivalent of the asp application object, assuming there is one. Making a query like this constantly for many users can be very heavy, resource-wise. – ClarkeyBoy Jul 28 '12 at 23:28
  • yes i know, but what i do to make it better? –  Jul 28 '12 at 23:29
  • I'm sorry I can't help you there - it has been a few years since I last did any PHP. If it was classic ASP or .net then I may have been able to help. – ClarkeyBoy Jul 28 '12 at 23:33

1 Answers1

0

You can also do this the other way around: instead of updating the time via ajax, you assume that the user is online until he leaves the page or closes the browser. In this case you use ajax to set the user as offline and execute it when browser fires onbeforeunload. In rare cases (power outage, system crash) this event will not be fired, so its a good idea to check also when the last update was made in case you have a floating user.

Anze Jarni
  • 1,141
  • 7
  • 7
  • Due to the rare cases you mention, this is not a good approach at all. Marking a user as online (either in the DB or in the application object) is the best approach and will achieve the most accurate results. Supposing your session timeout is set to 10 minutes, set the last seen time on page load and then set it again through ajax using `setTimeout(fn, 30000);` (correct me if I am wrong, that should be 5 minutes in milliseconds). – ClarkeyBoy Jul 29 '12 at 00:15
  • @ClarkeyBoy this rare cases can be handled via crontab cleaning. Although the approach of resetting online status every 5 minutes is not that server demanding, so I think it should also work. You could also use XMPP for this... – Anze Jarni Jul 29 '12 at 01:10
  • just simple when user login save session and count sessions in php – Mazhar Hussain Mar 29 '18 at 06:52