2012-12-06 175 views
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 
    } 
?> 
+1

的http://stackoverflow.com/questions/520237/how-do-i-expire-a-php-session-after-30-minutes – dtbarne

回答

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(); 
0
if ($_SESSION['timeout'] + 30 * 60 < time()) { 
    // 30 min timeout 
    }