【问题标题】:How do i log out user using session on php if inactive for 10 min [duplicate]如果不活动10分钟,我如何使用php上的会话注销用户[重复]
【发布时间】:2011-12-06 17:45:24
【问题描述】:

可能重复:
How do I expire a PHP session after 30 minutes?

这是我的注销脚本,如果页面处于非活动状态 10 分钟,我想注销用户并使用 header 发送到 index.php。

<?php
//initialize the session
if (!isset($_SESSION)) {
session_start();
}

// ** Logout the current user. **
$logoutAction = $_SERVER['PHP_SELF']."?doLogout=true";
if ((isset($_SERVER['QUERY_STRING'])) && ($_SERVER['QUERY_STRING'] != "")){
$logoutAction .="&". htmlentities($_SERVER['QUERY_STRING']);
$msg = "You have been logout successfully.";
}

if ((isset($_GET['doLogout'])) &&($_GET['doLogout']=="true")){
//to fully log out a visitor we need to clear the session varialbles
$_SESSION['MM_Username'] = NULL;
$_SESSION['MM_UserGroup'] = NULL;
$_SESSION['PrevUrl'] = NULL;
unset($_SESSION['MM_Username']);
unset($_SESSION['MM_UserGroup']);
unset($_SESSION['PrevUrl']);

$logoutGoTo = "index.php";
if ($logoutGoTo) {
header("Location: $logoutGoTo");
exit;
}
}
?>

【问题讨论】:

标签: php session logout


【解决方案1】:

这是通过设置会话超时来完成的。例如:

$idle = xxx; 
$session_life = time() - $_SESSION['timeout_logout'];
if($session_life > $idle)
{ 
session_destroy(); header("Location: index.php"); 
}
S_SESSION['timeout_logout']=time();

【讨论】:

    猜你喜欢
    • 2014-10-21
    • 1970-01-01
    • 2018-06-20
    • 2017-06-27
    • 1970-01-01
    • 1970-01-01
    • 2011-08-04
    • 1970-01-01
    • 2013-02-20
    相关资源
    最近更新 更多