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

后端 未结 12 2421
南笙
南笙 2020-12-06 02:19

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? A

相关标签:
12条回答
  • 2020-12-06 03:10

    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.

    0 讨论(0)
  • 2020-12-06 03:18

    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.

    0 讨论(0)
  • 2020-12-06 03:18

    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.

    0 讨论(0)
  • 2020-12-06 03:21

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

    Read this information about sessions

    0 讨论(0)
  • 2020-12-06 03:22

    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.

    0 讨论(0)
  • 2020-12-06 03:22

    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
    }
    
    0 讨论(0)
提交回复
热议问题