PHP - 如果在 10 分钟内没有任何操作,则销毁会话

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9049890/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 06:02:44  来源:igfitidea点击:

PHP - Destroy session if not any action in 10 minutes

phpsessionsession-timeout

提问by Eli_Rozen

Is there any option to destroy a session if user does not perform any action in 10 minutes?

如果用户在 10 分钟内未执行任何操作,是否可以选择销毁会话?

回答by ose

session_start();

// 10 mins in seconds
$inactive = 600; 

$session_life = time() - $_session['timeout'];

if($session_life > $inactive)
{  session_destroy(); header("Location: logoutpage.php");     }

S_session['timeout']=time();

The code above was taken from this particular page.

上面的代码取自这个特定页面。

回答by jmort253

Try setting the session timeout to 10 minutes.

尝试将会话超时设置为 10 分钟。

ini_set('session.gc_maxlifetime',10);

回答by YICT IR

I've done it with the following code:

我已经使用以下代码完成了它:

//10 min
if( !isset($_SESSION['logout']) ){
      $_SESSION['logout'] = strtotime('+10 minutes', time()); 
    }

    if( time() > $_SESSION['logout'])
    {
      session_destroy();
        header("Location: index.php"); 
    }else{
            $_SESSION['logout'] = strtotime('+10 minutes', time());
        }
    //echo date('Y/m/d h:i:s',$_SESSION['logout']);
    //echo $_SESSION['logout'];

回答by Aymen

i've modified the answer above, and it works fine :

我已经修改了上面的答案,它工作正常:

// inactive in seconds
$inactive = 10;
if( !isset($_SESSION['timeout']) )
$_SESSION['timeout'] = time() + $inactive; 

$session_life = time() - $_SESSION['timeout'];

if($session_life > $inactive)
{  session_destroy(); header("Location:index.php");     }

$_SESSION['timeout']=time();

回答by Priyabrat Rath

Including the following javascript in the page will cause a check for inactivity by calling the function CheckIdleTime()every second. Activity on the page resets _idleSecondsCounterto 0.

在页面中包含以下 javascript 将通过CheckIdleTime()每秒调用该函数来检查不活动状态。页面上的活动重置_idleSecondsCounter为 0。

<script type="text/javascript">
    var IDLE_TIMEOUT = 10 * 60;  // 10 minutes of inactivity
    var _idleSecondsCounter = 0;
    document.onclick = function() {
        _idleSecondsCounter = 0;
    };
    document.onmousemove = function() {
        _idleSecondsCounter = 0;
    };
    document.onkeypress = function() {
        _idleSecondsCounter = 0;
    };
    window.setInterval(CheckIdleTime, 1000);
    function CheckIdleTime() {
        _idleSecondsCounter++;
        var oPanel = document.getElementById("SecondsUntilExpire");
        if (oPanel)
            oPanel.innerHTML = (IDLE_TIMEOUT - _idleSecondsCounter) + "";
        if (_idleSecondsCounter >= IDLE_TIMEOUT) {
            // destroy the session in logout.php 
            document.location.href = "logout.php";
        }
    }
</script>

回答by Joseph

compare timestamps between two requests, one from the current request, one stored in the session.

比较两个请求之间的时间戳,一个来自当前请求,一个存储在会话中。