2012-12-06 174 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 
    }