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

前提是你 提交于 2019-11-26 20:58:38

问题


After the user successfully logs in, I store login = true in database. But how do I check if the user logged out by closing the browser without clicking the logout button? And also, 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 logged-in or not. But, now I want to store the info in database, so that I can display their status on other pages. Let's say user1 has 3 friends. When displaying all his friends, user1 want to know whether his friends are online or offline. This is what I want. Any advise?


回答1:


2017 edit: These days, your best bet is using websockets to track presence on a page/site.


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.



回答2:


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.




回答3:


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.




回答4:


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.




回答5:


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




回答6:


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.




回答7:


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.




回答8:


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

Read this information about sessions




回答9:


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.




回答10:


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
}



回答11:


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.




回答12:


You can do this with a combination of ways, 2 at the most i guess, with the one bothering on last activity, you combine it with using jQuery to check for inactivity, that is no mouse or keyboard events for some time, say about 10 to 20 mins, then once the idle time is confirmed, you make an ajax call to a php file that will update your database table showing user offline.

You can start with:

<script type="text/javascript">
idleTime = 0;
$(document).ready(function () {
//Increment the idle time counter every minute.
var idleInterval = setInterval(timerIncrement, 60000); // 1 minute

//Zero the idle timer on mouse movement.
$(this).mousemove(function (e) {
    idleTime = 0;
});
$(this).keypress(function (e) {
    idleTime = 0;
});
});

function timerIncrement() {
idleTime = idleTime + 1;
if (idleTime > 19) { // 20 minutes
    $.ajax({
     url: 'update_user.php',
     type: 'POST',
     datatype: 'json',
     data: someData
    });
}
}
</script>   


来源:https://stackoverflow.com/questions/887919/how-to-detect-if-a-user-has-logged-out-in-php

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!