3

Could anyone tell me why this doesn't work? In my database lastactive is 2013-12-10 16:15:12, updates every time a user refreshes any page on my website.

I select it and set it as a variable:

$lastactive = $row[5]; 

Here's where I thought it should work, but doesn't. Using 10 seconds for testing.

if(time() > $lastactive+10){
    print('<div id="user_online_status" style="color: #aaa;">[OFFLINE]</div>');
}
else if(time() < $lastactive+10){
    print('<div id="user_online_status">[ONLINE]</div>');
}
Sam
  • 7,252
  • 16
  • 46
  • 65
user3010610
  • 51
  • 1
  • 7
  • 1
    You're selecting out a time **STRING**, meaning you're testing `if(123456 > '2013-12-10 hh:mm:ss')`... look at that for a while and try to figure out why it'd never work. – Marc B Dec 10 '13 at 20:23

4 Answers4

4

You're comparing a unix timestamp to a MySQL datetime string. You need to convert it to a unix timestamp before comparing the two:

$lastactive = strtotime($row[5]);
John Conde
  • 217,595
  • 99
  • 455
  • 496
3

Replace your SELECT statement from:

SELECT lastOnline FROM user

to something like...

SELECT UNIX_TIMESTAMP(lastOnline) FROM user

that's it. You're currently checking the Date string against a UNIX Timestamp.

Robin Heller
  • 400
  • 5
  • 14
2

I dont see its good idea to check for time.

What if user doesnt refresh the page , he let the page open and went to eat ? . he will be loggedout? it will be anonying.

I guess better is to use unload

   $(window).unload(function() {
      // Send an Ajax request to logout.php
 });
echo_Me
  • 37,078
  • 5
  • 58
  • 78
2

if user doesnt refresh the page, you can check it on server using cron.

$limit = $userOnline+60; // cron set to run every minute

if($userOnline < $limit)
  $userStatus = "offline";
else
  $userStatus = "online";
John Conde
  • 217,595
  • 99
  • 455
  • 496
Azam
  • 21
  • 1