设置会话超时

时间:2012-12-06 03:20:57

标签: php sql

我有一个用于管理员和普通用户登录的表单。我想在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
    }
?>

3 个答案:

答案 0 :(得分: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();

答案 1 :(得分:1)

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

答案 2 :(得分:0)

if ($_SESSION['timeout'] + 30 * 60 < time()) {
     // 30 min timeout
  }