0

我有一个用于管理员和普通用户登录的表单。我想在 30 分钟后将会话超时,我将如何修改我当前的代码来做到这一点?

<?php
    session_start(); //Start the session
    define(ADMIN,$_SESSION['username']); //Get the user name from the previously registered super global variable

    if(!session_is_registered("admin")){ //If session not registered
        header("location:../index.php"); // Redirect to login.php page
    }
?>
4

3 回答 3

3

这是清除指定时间会话的代码。

要清除非活动会话,您必须每页更新会话超时。希望能帮助到你。

供参考,http://bytes.com/topic/php/insights/889606-setting-timeout-php-sessions

session_start();
$timeout = 60; // Number of seconds until it times out.

// Check if the timeout field exists.
if(isset($_SESSION['timeout'])) {
    // See if the number of seconds since the last
    // visit is larger than the timeout period.
    $duration = time() - (int)$_SESSION['timeout'];
    if($duration > $timeout) {
        // Destroy the session and restart it.
        session_destroy();
        session_start();
    }
}

// Update the timout field with the current time.
$_SESSION['timeout'] = time();
于 2012-12-06T03:38:07.323 回答
1

通过运行时配置项session.cookie_lifetime或函数session_set_cookie_params()

于 2012-12-06T03:28:13.010 回答
0
if ($_SESSION['timeout'] + 30 * 60 < time()) {
     // 30 min timeout
  } 
于 2012-12-06T03:28:18.270 回答