timed auto logout and browser close

后端 未结 6 1165
温柔的废话
温柔的废话 2021-01-19 15:29

I\'ve created a very simple multiuser game for learning purposes.

As users log on, each other user gets an update of all currently logged in users.

When a us

相关标签:
6条回答
  • 2021-01-19 15:56

    I assume that, users are shown as online and offline on logout. Destroying session or cookie will require client browser in working mode.

    Solution

    I also assume there is a timestamp column maintained. Set an interval to check time-gap between current TS and Last Timestamp. Also update your own TS against your id to current TS.

    setInterval(function(){ 
        $.ajax({
            url: "/backend.php?userid=id",
            success: function(resoponse){
                console.log(response);
            }
        });
    }, 10000);
    

    backend.php

    $user_id = $_GET["userid"];
    $query = "select * from table name where (GETDATE() - timestamp) > 60";
    $resut = mysqli_query($con, $query);
    while($row = mysqli_fetch_assoc($result)){
        //set there status = 0 as offline
        $other_user_id = $row['user_id'];
        mysqli_query($con, "update tablename set status = 0 where user_id = '$other_user_id'");
    }
    //update your own timestamp
    mysqli_query($con, "update tablename set timestamp = GETDATE() where user_id='$user_id'");
    

    This would check user activity, or basically check if that js is running or not. If browser was closed then TS for that user won't get updated hence GETDATE() - timestamp will be greater than 60. Now other users who would be running the web app would also run the same script, checking and updating all users status as per condition. When user closes his tab he would still be online for at least 1 minute.

    I had a similar problem and found solution here PHP auto logout on tab close.

    0 讨论(0)
  • 2021-01-19 16:01

    You could implement a $_SESSION based solution, like Gumbo answered this question. Then you could adapt it to handle your database requirements.

    0 讨论(0)
  • 2021-01-19 16:01

    Instead of just setting a flag when a user logs in, log a timestamp you can use to determine the time of log-in. Also, you can have your client, call-home using AJAX some PHP script to save in the database the timestamp of last user-activity.

    You can then execute a session cleaner script periodically using a cron job in the server. The script would query the database to detect the stale sessions and do the log-off (set the logged on flag to 0). If the server has PHP CLI (Command Line Interface) installed, you can code the script in PHP.

    0 讨论(0)
  • 2021-01-19 16:02

    if you used html5 socket connections as your way of connecting to the server, your socket server script would know when a subscriber disconnects. this would also allow you to push data to all the clients in real time without long polling. the only problem is I don't know what the support of html5 sockets is at the moment. I have done something similar using Flash, which also supports socket connections.

    0 讨论(0)
  • 2021-01-19 16:04

    Don't know about the first question, but how about a suggestion on the second:

    When the user is 'active', will they be causing page requests fairly regularly? If so, you could have a system whereby a logged-on user is periodically logged in your SQL database, with a timestamp. Then have another script look up all those users whose timestamps are older than a specified period (whatever you like, eg 10 seconds) and have them set to '0'.

    I could suggest some code but maybe this isn't what you're looking for - let me know.

    EDIT: OK, the link from your edit seems to have been answered with a similar system to what I just suggested. If you use AJAX you can call a php script periodically in the background to set and check timestamps from the SQL table.

    0 讨论(0)
  • 2021-01-19 16:09

    A possible way to only catch a user who comes back to the site after they closed the window could be using a database for user ID, IP and time like so:

    $_SESSION['ip'] = $_SERVER['HTTP_CLIENT_IP'];
    if(isset($_SESSION['ip'])){
        if(Logedin){ // This login detection can be set by cookies can very depending on situation
            mysql_query("INSERT INTO users SET user_login=\"".'1'."\",user_id=\"".$idfromlogin."\",user_ip=\"".$_SESSION['ip']."\",user_time=\"".time();."\"");
        }
    

    Then we can check this every time a page loads against the time(); if the user closes window by "X" button the session is over. Now we have to start a new session but that's why we saved the time in the database.

    $_SESSION['ip'] = $_SERVER['HTTP_CLIENT_IP'];
    if(isset($_SESSION['ip'])){
        if (!$sql = mysql_query("SELECT * FROM users WHERE user_ip=".$_SESSION['ip'])){
            die('Error: ' . mysql_error());
        }
    
        $row = mysql_fetch_array($sql);
        if ($row['user_time'] + 10 * 60 < time()){ // this is 10 minutes have passed
            // now to find the user database by the session database the logins is your method of loging in a user from logins database with pass and name
            if (!$user = mysql_query("UPDATE logins SET user_isloggedin=\"".'0'."\" WHERE user_id=".$row['user_id'])){ //from the users database
                die('Error: ' . mysql_error());
            }
        }
    

    This only shows a way to check IP if they came back and the recorded time has passed previous. This does not show how to catch when some one presses "X" button. IP's can change so I would write a check IP function:

    function getRealIpAddr(){
        if (!empty($_SERVER['HTTP_CLIENT_IP'])){   //check IP from share internet
            $ip=$_SERVER['HTTP_CLIENT_IP'];
        } elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])){   //to check IP is pass from proxy
            $ip=$_SERVER['HTTP_X_FORWARDED_FOR'];
        }else{
            $ip=$_SERVER['REMOTE_ADDR'];
        }
        return $ip;
    }
    
    0 讨论(0)
提交回复
热议问题