關於How do I expire a PHP session after 30 minutes?,我從第二個答案複製了一些代碼在30分鐘內PHP會話過期的簡單方法。我想結合登錄和信息到1頁,另一頁是logout.php 這裏是我的代碼。php會話超時所有代碼在一頁中
homepage.php
if(isset($_POST["submitform"])){
$v1 = "admin";
$v2 = "admin";
$v3 = $_POST['username'];
$v4 = $_POST['password'];
if($v1 == $v3 && $v2 == $v4){
session_start();
$_SESSION['username'] = $v1;
$_SESSION['start'] = time(); // taking now logged in time
$_SESSION['expire'] = $_SESSION['start'] + (1* 30) ; // ending a session in 30 seconds
if(!isset($_SESSION['username'])){
echo "Please Login again <a href='logout.php'>Click Here to Login</a>";
}else{
$now = time(); // checking the time now when home page starts
if($now > $_SESSION['expire']){
session_destroy();
echo "Your session has expire ! <a href='logout.php'>Click Here to Login</a>";
}else{
echo "This should be expired in 1 min <a href='logout.php'>Click Here to Login</a>";
}
}
}else{
echo '
<form method="post">
<input type="text" name="username">
<input type="password" name="password">
<button type="submit" name="submitform">Sign in</button>
</form>';
echo '<font color="red">wrong password</font>"';
}
}else{
echo '
<form method="post">
<input type="text" name="username">
<input type="password" name="password">
<button type="submit" name="submitform">Sign in</button>
</form>';
}
?>
Logout.php
<?php
session_start();
session_destroy();
header('Location: homepage.php');
?>
我設置會話過期30秒,但是我發現預期會議不會過期。會議永不過期。我想知道如果我把session_start();
放在正確的地方?謝謝
你是怎麼意思永不過期的?你是否已經離開頁面超過30分鐘,然後回來發現自己仍然登錄? – 2013-04-27 19:41:37
@PhillSparks。 30秒30分鐘,縮短了到期時間。是的,我仍然登錄。 – olo 2013-04-27 19:42:20
簡單地檢查$ _SESSION ['expire']與時間,並且如果時間更大,銷燬會話,我從來沒有親自看到時間小於5分鐘的餅乾 – Hawili 2013-04-27 19:56:11