views:

32

answers:

4

Hi! I want to set up online detection on my website.

I have a row in my user table where the last login datetime is stored. Every time a user visits the site, his login date updates and user online row sets to 1 (1 - online, 0 - offline).

How to change the online row to 0 (offline) if the last login was 10 or more minutes ago? The aim is to find difference between dates.

+2  A: 

cronjob every 10 minutes?

UDPATE users SET online = 0 WHERE login_date > (NOW() - INTERVAL 10 minute);
Bas van Dorst
There is no need for cron. I've got a header.php included in every page that will do queries.
Denis Bobrovnikov
Cron's should mainly be used for server side stuff, not really app specific.
RobertPitt
Doing queries like this is practical for small sites or testing, but do your users a favor and change it to a cron job if you actually start getting a bit of users - otherwise you page load time will be sporadic and lengthy if you are forcing the user's connection to handle the burden of doing your database cleanup for you.
methodin
@RobertPitt Not really. Crons are used in a wide variety of ways, mainly to disperse load of lengthy operations (database queries) amongst servers to avoid making your website appear slow and sluggish by implementing it in your suggested way.
methodin
Lets say there was a chat system implemented into this social system, this would have a dramatic effect on who's on line and who's off line though.
RobertPitt
A: 

just to each user add a last_seen timestamp to there row so that when you do your user is authed check you can update the time

if(logged_in())
{
    update_user();
}


function update_user()
{
    //UPDATE users SET last_seen = unix_timestamp() WHERE uid = X;
}

Then you can do for you users:

SELECT * FROM users WHERE last_seen > (unix_timestamp()-300)

To get the last 5 mins.

RobertPitt
The full login function looks like this function login($u, $p){ $u = urldecode($u); $query = mysql_query("SELECT * FROM tbl_users WHERE user_email = '$u' AND user_password = '$p' LIMIT 1"); if(mysql_num_rows($query)){ $user = mysql_fetch_array($query); setcookie('name', urlencode($user['user_email']), time() + 60*60*24*30); setcookie('pass', $user['user_password'], time() + 60*60*24*30); mysql_query("UPDATE tbl_users SET user_login_date = NOW() WHERE user_username = '$u'"); return mysql_result($query, 0); } else return false;}
Denis Bobrovnikov
there's a very large security flaw there.
RobertPitt
A: 

If you want to show the users who have been online within last 10 mins then the best method is to include the datetime condition in the sql query.

Ashwini Dhekane
A: 

Save the last login time as timestamp, then you can easily compare it with the current time and tell how much time has passed since.

Depending on the size of your user table, you can run the check of those who are still supposedly online every time somebody calls your website.

A different approach is to store active users in buckets, labeled with the last login time, you can then easily reset all users that are in buckets older than 10 minutes.

bitmask