如何在30分钟后使PHP会话到期?我需要让会话保持30分钟,然后将其销毁。
5 回答
![?](http://img1.sycdn.imooc.com/545868b60001587202200220-100-100.jpg)
森林海
TA贡献2011条经验 获得超2个赞
我明白上面的答案是正确的,但它们是在应用程序级别,为什么我们不只是使用.htaccess
文件来设置过期时间?
<IfModule mod_php5.c> #Session timeout php_value session.cookie_lifetime 1800 php_value session.gc_maxlifetime 1800</IfModule>
![?](http://img1.sycdn.imooc.com/533e51f30001edf702000200-100-100.jpg)
波斯汪
TA贡献1811条经验 获得超4个赞
if (isSet($_SESSION['started'])){ if((mktime() - $_SESSION['started'] - 60*30) > 0){ //Logout, destroy session, etc. }}else { $_SESSION['started'] = mktime();}
![?](http://img1.sycdn.imooc.com/533e4d470001a00a02000200-100-100.jpg)
红糖糍粑
TA贡献1815条经验 获得超6个赞
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');?>
![?](http://img1.sycdn.imooc.com/545850a00001fdd002200220-100-100.jpg)
Helenr
TA贡献1780条经验 获得超4个赞
这是在一段时间后将用户注销吗?设置会话创建时间(或到期时间),然后检查每个页面上的负载可以处理它。
例如:
$_SESSION['example'] = array('foo' => 'bar', 'registered' => time());// laterif ((time() - $_SESSION['example']['registered']) > (60 * 30)) { unset($_SESSION['example']);}
编辑:我觉得你的意思是别的。
您可以使用session.gc_maxlifetime
ini设置在一定生命周期后删除会话:
编辑: ini_set('session.gc_maxlifetime',60 * 30);
- 5 回答
- 0 关注
- 603 浏览
添加回答
举报
0/150
提交
取消