Rectangle 27 0

mysql How to detect if a user has logged out, in php?


  • Create a column in your user table along the lines of 'last_activity'.
  • To get a list of who's online, just query the database for users with last_activity values more recent than 10/20/whatever minutes ago.
  • Whenever a user loads a page, update their last_activity to the current time.

@show_lol, To prevent the potential load on the db you can keep track of the last time you updated your database. For instance, if it has been less than one minute since the last db update, it might not be necessary to update it again.

If your site is so busy that an extra query per page bogs down your system, a "who's online?" page will be so huge as to be useless anyways. Gumbo's session method won't work for a "who's online?" page.

This looks like, it will make the database very busy if every page visited need to update database. is there any light way like Gumbo suggested? I am not very get his comment, not very understand...

You cannot detect when a user closes their browser or navigates off your site with PHP, and the JavaScript techniques of doing so are so far from guaranteed as to be useless.

i used the method you described for a project once, and it works great. you can even use the mysql date and time functions, like timediff() and (time_to_sec(timediff(now(), last_activity))) to show the period (last activity: 15 minutes ago).

Note
Rectangle 27 0

mysql How to detect if a user has logged out, in php?


session_start();
if (!isset($_SESSION['LAST_ACTIVITY'])) {
    // initiate value
    $_SESSION['LAST_ACTIVITY'] = time();
}
if (time() - $_SESSION['LAST_ACTIVITY'] > 3600) {
    // last activity is more than 10 minutes ago
    session_destroy();
} else {
    // update last activity timestamp
    $_SESSION['LAST_ACTIVITY'] = time();
}

But if you want to get a list of currently online users, you need a source you have permanently access to from everywhere. So youre better off with the database solution.

Store the timestamp of each acitivity of the user. When that time is more than 10 minutes ago, do the logout.

The comments describe the meaning of each branch. So last activity is more than 10 minutes ago means that this branch is entered if the last activity is more than 10 minutes ago, so he was more than 10 minutes inactive. What that means for your application logic is your business.

The same can be done on the database instead. There you could even get a list of users that are currently online.

opps, sory.. i know, if last activity more than 10 minutes, i will redirect to a php page, tell my database that this user has been logged out? is this what u mean?

thanks gumbo, but i am not very understand what u mean. A source that permanently access to everywhere? Sorry, i am quite new to php and quite noob about php

Note
Rectangle 27 0

mysql How to detect if a user has logged out, in php?


To be safe you should track the user's last activity and if it's past a few minutes (5/10) then assume that user is gone. If he does something again though (after 6 minutes for example) then he's back online.

You are wondering if you can detect if a user closed his browser. You kinda can with javascript but I would not rely on it (since javascript is easy to disable) But you can not with PHP, since PHP only runs when you are requesting a page. Not when the page is open.

Note
Rectangle 27 0

mysql How to detect if a user has logged out, in php?


To be safe you should track the user's last activity and if it's past a few minutes (5/10) then assume that user is gone. If he does something again though (after 6 minutes for example) then he's back online.

You are wondering if you can detect if a user closed his browser. You kinda can with javascript but I would not rely on it (since javascript is easy to disable) But you can not with PHP, since PHP only runs when you are requesting a page. Not when the page is open.

Note
Rectangle 27 0

mysql How to detect if a user has logged out, in php?


  • Create a column in your user table along the lines of 'last_activity'.
  • To get a list of who's online, just query the database for users with last_activity values more recent than 10/20/whatever minutes ago.
  • Whenever a user loads a page, update their last_activity to the current time.

@show_lol, To prevent the potential load on the db you can keep track of the last time you updated your database. For instance, if it has been less than one minute since the last db update, it might not be necessary to update it again.

If your site is so busy that an extra query per page bogs down your system, a "who's online?" page will be so huge as to be useless anyways. Gumbo's session method won't work for a "who's online?" page.

This looks like, it will make the database very busy if every page visited need to update database. is there any light way like Gumbo suggested? I am not very get his comment, not very understand...

You cannot detect when a user closes their browser or navigates off your site with PHP, and the JavaScript techniques of doing so are so far from guaranteed as to be useless.

i used the method you described for a project once, and it works great. you can even use the mysql date and time functions, like timediff() and (time_to_sec(timediff(now(), last_activity))) to show the period (last activity: 15 minutes ago).

Note
Rectangle 27 0

mysql How to detect if a user has logged out, in php?


session_start();
if (!isset($_SESSION['LAST_ACTIVITY'])) {
    // initiate value
    $_SESSION['LAST_ACTIVITY'] = time();
}
if (time() - $_SESSION['LAST_ACTIVITY'] > 3600) {
    // last activity is more than 10 minutes ago
    session_destroy();
} else {
    // update last activity timestamp
    $_SESSION['LAST_ACTIVITY'] = time();
}

But if you want to get a list of currently online users, you need a source you have permanently access to from everywhere. So youre better off with the database solution.

Store the timestamp of each acitivity of the user. When that time is more than 10 minutes ago, do the logout.

The comments describe the meaning of each branch. So last activity is more than 10 minutes ago means that this branch is entered if the last activity is more than 10 minutes ago, so he was more than 10 minutes inactive. What that means for your application logic is your business.

The same can be done on the database instead. There you could even get a list of users that are currently online.

opps, sory.. i know, if last activity more than 10 minutes, i will redirect to a php page, tell my database that this user has been logged out? is this what u mean?

thanks gumbo, but i am not very understand what u mean. A source that permanently access to everywhere? Sorry, i am quite new to php and quite noob about php

Note
Rectangle 27 0

mysql How to detect if a user has logged out, in php?


If you are trying to track the users that are 'online' then you might consider using a session for the individual user and instead of storing login=true in the db to display their status to you or others, store the last activity time for the user. When you pull up your list of online users, create your sql query to only return users with 'last_activity' within the last 10 minutes.

Note
Rectangle 27 0

mysql How to detect if a user has logged out, in php?


If you are trying to track the users that are 'online' then you might consider using a session for the individual user and instead of storing login=true in the db to display their status to you or others, store the last activity time for the user. When you pull up your list of online users, create your sql query to only return users with 'last_activity' within the last 10 minutes.

Note
Rectangle 27 0

mysql How to detect if a user has logged out, in php?


$user = new User($user_id);
$user->logged_in = (bool)($last_activity > 0);

IMHO the best way is to store last activity timestamp in DB on each update of user record. After logoff or timeout (maintain timeouts with cronjob) just set it to zero-value and use as flag.

Sometimes you will need to say smth. like "last seen on ...", then leave last activity and just add a boolean flag (tinyint) logged_in to your users table.

Note
Rectangle 27 0

mysql How to detect if a user has logged out, in php?


$user = new User($user_id);
$user->logged_in = (bool)($last_activity > 0);

IMHO the best way is to store last activity timestamp in DB on each update of user record. After logoff or timeout (maintain timeouts with cronjob) just set it to zero-value and use as flag.

Sometimes you will need to say smth. like "last seen on ...", then leave last activity and just add a boolean flag (tinyint) logged_in to your users table.

Note