如何在30分钟后注销无效用户?

问题描述:

我正在尝试注销非活动用户。如果他们没有移动鼠标或在30分钟内输入,则会自动注销并返回登录屏幕。登录详细信息存储在我的数据库中。

I am trying to log out inactive users. If they have not moved the mouse or typed in 30 minutes then they are automatically logged out and returned to the login screen. The logon details are stored in my database.

登录例程

<?php
require_once('includes/session.php');
    if (isset($_GET['logout']) and $_GET['logout'] == 1){
        logout();
    }
    //doublecheck status
    if (isset($_SESSION['user_id'])){
        $login = 1; $login_message = "Logged In";
    }else{
        $login = 0; $login_message = "Logged Out";
    }

include_once("../includes/masterinclude.php");
//include_once("../includes/functions_admin.php");


$preferences = getPreferences();
    $ip=$_SERVER['REMOTE_ADDR'];
    $message_login = "";

    if (isset($_POST['username']) and isset($_POST['password'])){
        $username = $_POST['username'];
        $password = $_POST['password'];
        $hashed_password = sha1($password);
        $u = Confirm_User($username, $hashed_password);
        if ($u == 1){
            $u = Get_User($username, $hashed_password);
            $_SESSION['user_id'] = $u->user_id;
            $_SESSION['username'] = $u->user_name;
            echo "<script type=\"text/javascript\">document.location.href=\"/home\";</script>";
        }else{
            $warning = "red";
            $message_login = "Login failed - Please try again";
        }
    }
?><head>



<form id="login" name="login" class="form-horizontal" method="post" action="_cms/login.php" _cms/style="display: block;">
            <div class="form-group" id="reauthorizeInner">
                <?php
                if($message_login != ""){
                    echo "<p><span class=\"message-error\">" . $message_login . "</span></p>";
                }else{
                    echo "<p class=\"message\">Please enter your username &amp; password</p>";
                }
                ?>
                <div class="input-group col-xs-12">
                    <input id="reauthuser" class="form-control" type="text" placeholder="Username.." name="username" value="username" onFocus="this.value=''" required="yes" message="You must enter a username">
                    <span class="input-group-addon">
                    <i class="icon-envelope-alt icon-fixed-width"></i>
                    </span>
                    </div>
            </div>
            <div class="form-group" id="reauthorizeInner">
                <div class="input-group col-xs-12">
                    <input id="reauthPassword" class="form-control" name="password" type="password" value="password" onFocus="this.value=''" required="yes" message="You must enter a password">

                    <span class="input-group-addon">
                    <i class="icon-asterisk icon-fixed-width"></i>
                    </span>
                </div>
            </div>
            <div class="clearfix">
                <div class="btn-group btn-group-sm pull-right">

                    <button class="btn btn-primary" id="submit" type="submit" onclick="document['login'].submit();">
                        <i class="icon-arrow-right"></i>
                        Login
                    </button>
                </div>
                <div class="make-switch pull-left" data-on="primary" data-off="danger"></div>
            </div>
        </form>

Session.php

Session.php

    <?php
session_start();
if (isset($_SESSION['user_id'])){
    $login = 1;
}else{
    $login = 0;
}

function confirm_logged_in() {
    if (!isset($_SESSION['user_id'])) {
        //redirect
        header("Location: /_cms/login.php?login=0");
    }
}
function logout(){
        $_SESSION = array();
        if(isset($_COOKIE[session_name()])){
            setcookie(session_name(), '', time()-42000, '/');
        }   
        session_destroy();
}

?>

我知道这里有很多代码,但我必须包含这个,否则人们不会看到复杂性为此创建一个注销计时器。我尝试了一些不同的方法,但由于登录例程的错误方式,它们都没有工作。任何帮助将不胜感激!

I know there is alot of code here but I have to include this otherwise people will not see the complexity of creating a logout timer for this. I have tried a few different methods and none of them work because of the way the login routine is wrong. Any help would be greatly appreciated!

我会使用一些JS / jQuery和 iddletimout库并将其与您的PHP代码结合使用:

I would use some JS/jQuery and iddletimout library and combine it with your PHP code:

$.idleTimeout('#idletimeout', '#idletimeout a', {
        idleAfter: 300, //seconds
        onTimeout: function() {
           //some code
           window.location = "logout.php"; //This is your PHP logout page
        },
        onIdle: function() {
            //some code
        },
        onCountdown: function(counter) {
            //some code
        },
        onResume: function() {
            //some code
        }
    });