views:

1095

answers:

13

After the user successfully logged in, I store login = true in database. But how do I check if the user logged out by close the browser without click the logout button? And somemore, how do I redirect user who has been inactive for 10 minutes to login page?

I am using php and mysql. Any help would be appreciated.

EDIT: Sorry if my question is not clear. I did use session to store whether they are loggin or not. But, now i want to store the info in database, so that i can display their status on other pages. Lets user1 has 3 friends. When display all his friends, he want to know whether his friend is online or offline. This is what I want. Any advise?

A: 

It might be better to store login=true in a session variable to check wheter or not the user is logged in. This would solve most of your problems ;)

Ropstah
A: 

Normally you would put such information in sessions.

$_SESSION['user'] = "A user name";

Then when you want to logout you do:

session_destroy();

Some more info on sessions here and a tutorial.

MrHus
i know. But if the user close the browser? My database still remain the status is true right? I want to display whether the user is online or offline.
The status should be a datetime then every time the user does something you could update this field. If the user has been active in the last half hour you could say he is logged in. Or something similar.
MrHus
A: 

Why dont you use session or cookies for this instead of inserting it in the database. You can set the cookies by using setcookie() function or either you can make a session vaiable and store the value in it.

developer
A: 

You would find it better to use the session to monitor the login status.

Read this information about sessions

gacrux
+6  A: 

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.

Instead, your best bet is most likely to store each user's last activity time.

  • Create a column in your user table along the lines of 'last_activity'.
  • Whenever a user loads a page, update their last_activity to the current time.
  • 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.
ceejayoz
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).
Schnalle
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...
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.
ceejayoz
@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.
Haluk
+1  A: 

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.

Eddy
A: 

AFAIK there is no way for you to check when a person closes the browser window (or leaves you page to go to another) so you need to check activity as others suggested above.

Michal M
+1  A: 

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.

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.

Ólafur Waage
+4  A: 

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

In PHP, you could do something like this:

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();
}

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

Gumbo
is this applicable for more than 1 user? If i want to list 10 users, want to check 10 users are online, how to do it?
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?
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.
Gumbo
But if you want to get a list of “currently” online users, you need a source you have permanently access to from everywhere. So you’re better off with the database solution.
Gumbo
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
In a database you can always access all data sets at the same time. But in a session you can only access one data set at a time: the one that is associated with the session ID.
Gumbo
A: 

This is an extension to what 'ceejayoz' said before.

Let the users periodically ping the service and tell that they are still logged in. Store last ping time in the session. If the last ping session is greater than a said time, then ping again to tell that the use is still alive.

Store the time when you received the ping in the database. If the last ping received is > the keeplive threshold then consider the user has left the site.

The following is some untested sample code for you start with. You can use the "PING__FREQUENCY" to control how often the frequency in which the user activity will update the last_activity column.

define(PING_FREQUENCY,300); //5 mins
if (($_SESSION['lastPingTime'] + PING_FREQUENCE) > time()) {
    stillLoggedIn(); //execute a function to tell that the user is still logged in
}

function stillLoggedIn() {
    //Do SQL update to mark the last activity time for the user
}
StackKrish
caveat: you can DDoS your site this way, if you have a large number of users
Piskvor
True. This is not something that I would do btw. But only offered as a solution to the problem described in the Original Post.
StackKrish
A: 

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.

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

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.

Jet
A: 

But how to make show logout user right after the close browser .

harish