-
森林海
我明白上面的答案是正确的,但它们是在应用程序级别,为什么我们不只是使用.htaccess文件来设置过期时间?<IfModule mod_php5.c>
#Session timeout
php_value session.cookie_lifetime 1800
php_value session.gc_maxlifetime 1800</IfModule>
-
波斯汪
if (isSet($_SESSION['started'])){
if((mktime() - $_SESSION['started'] - 60*30) > 0){
//Logout, destroy session, etc.
}}else {
$_SESSION['started'] = mktime();}
-
红糖糍粑
PHP会话在30分钟内到期的简单方法。注意:如果你想改变时间,只需用你想要的时间改变30,不要改变* 60:这将给出分钟。在几分钟内:(30 * 60)在几天内:(n * 24 * 60 * 60)n =没有天数 的login.php<?php
session_start();?><html>
<form name="form1" method="post">
<table>
<tr>
<td>Username</td>
<td><input type="text" name="text"></td>
</tr>
<tr>
<td>Password</td>
<td><input type="password" name="pwd"></td>
</tr>
<tr>
<td><input type="submit" value="SignIn" name="submit"></td>
</tr>
</table>
</form></html><?php if (isset($_POST['submit'])) {
$v1 = "FirstUser";
$v2 = "MyPassword";
$v3 = $_POST['text'];
$v4 = $_POST['pwd'];
if ($v1 == $v3 && $v2 == $v4) {
$_SESSION['luser'] = $v1;
$_SESSION['start'] = time(); // Taking now logged in time.
// Ending a session in 30 minutes from the starting time.
$_SESSION['expire'] = $_SESSION['start'] + (30 * 60);
header('Location: http://localhost/somefolder/homepage.php');
} else {
echo "Please enter the username or password again!";
}
}?>HomePage.php<?php
session_start();
if (!isset($_SESSION['luser'])) {
echo "Please Login again";
echo "<a href='http://localhost/somefolder/login.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 expired! <a href='http://localhost/somefolder/login.php'>Login here</a>";
}
else { //Starting this else one [else1]?>
<!-- From here all HTML coding can be done -->
<html>
Welcome <?php
echo $_SESSION['luser'];
echo "<a href='http://localhost/somefolder/logout.php'>Log out</a>";
?>
</html><?php }
}?>LogOut.php<?php
session_start();
session_destroy();
header('Location: http://localhost/somefolder/login.php');?>
-
Helenr
这是在一段时间后将用户注销吗?设置会话创建时间(或到期时间),然后检查每个页面上的负载可以处理它。例如:$_SESSION['example'] = array('foo' => 'bar', 'registered' => time());// laterif ((time() - $_SESSION['example']['registered']) > (60 * 30)) {
unset($_SESSION['example']);}编辑:我觉得你的意思是别的。您可以使用session.gc_maxlifetimeini设置在一定生命周期后删除会话:编辑: ini_set('session.gc_maxlifetime',60 * 30);