2015-02-08 86 views
-1

我寫了一些代碼來超時會話;當空閒不工作時會話超時php

<?php 

session_start(); 
// set timeout period in seconds 
$inactive = 10; 
// check to see if $_SESSION['timeout'] is set 
if(isset($_SESSION['timeout'])) { 
$SESSION_life = time() - $_SESSION['timeout']; 
if($SESSION_life > $inactive) 
{ session_destroy(); header("Location: login.php");exit; } 
} 
$_SESSION['timeout'] = time(); 


if (isset($_SESSION['username'])) { 

    echo "<center>Welcome </center>" ; //  echo "<p> </p>"; 
    echo " <center>". $_SESSION['username']. "</center>" ; 
    echo "<br /><center>".$_SESSION["role"]."<br /></center>" ; 

}else{ 

    header("location:login.php"); 

} 

但是,如果會話閒置10秒,會話不會超時。

+0

把'exit;'放在你的重定向之後。 – zerkms 2015-02-08 20:26:54

+0

已添加,但仍然相同。 – suppko 2015-02-08 20:35:28

+0

如果您修改了代碼 - 相應地更改了問題。 – zerkms 2015-02-08 20:39:05

回答

1

看起來像你幾乎那裏。我會試試這個:

<?php 

session_start(); 

$inactive_time = 10; 

if(isset($_SESSION['last_active_time'])){ 

    $time_since_last_active = time() - $_SESSION['last_active_time']; 

    if($time_since_last_active >= $inactive_time){ 
     // The user has been inactive for too long 
     session_destroy(); 
     header('Location: login.php'); 
     exit; 
    }else{ 
     // Set the last active tim 
     $_SESSION['last_active_time'] = time(); 
    } 

}else{ 
    $_SESSION['last_active_time'] = time(); 
} 
+0

mm我嘗試了你發佈的內容,但它仍然無法正常工作。 – suppko 2015-02-08 20:35:05

+0

適合我的作品 - 你是否等待10秒? – ajtrichards 2015-02-08 20:39:00

+0

是的,我沒等。我甚至將$ inactive_time更改爲3秒,但仍然沒有重定向到login.php – suppko 2015-02-08 20:41:16