我需要保持一个会话存活30分钟,然后销毁它。
当前回答
PHP会话在30分钟内到期的简单方法。
注意:如果你想改变时间,只需要改变30和你想要的时间,不要改变* 60:这将给出分钟。
分钟:(30 * 60) (n * 24 * 60 * 60) n = no天
login。
<?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');
?>
其他回答
这让我大开眼界,克里斯托弗·克莱默在2014年写的 https://www.php.net/manual/en/session.configuration.php#115842
在debian(基于)系统上,更改会话。Gc_maxlifetime在运行时没有实际影响。Debian通过设置session.gc_probability=0禁用PHP自己的垃圾收集器。相反,它有一个每30分钟运行一次的cronjob(参见/etc/cron.d/php5)来清理旧的会话。这个cronjob基本上会查看你的php.ini并使用session的值。Gc_maxlifetime来决定清理哪些会话(参见/usr/lib/php5/maxlifetime)。[…]
if (isSet($_SESSION['started'])){
if((mktime() - $_SESSION['started'] - 60*30) > 0){
//Logout, destroy session, etc.
}
}
else {
$_SESSION['started'] = mktime();
}
PHP是如何处理会话的,初学者很难理解。这可能会帮助他们概述会话的工作原理: 会话如何工作(自定义会话处理程序)
这是为了在设定的时间后注销用户吗?在注册会话时设置会话创建时间(或过期时间),然后检查每个页面上的加载是否可以处理该时间。
例如:
$_SESSION['example'] = array('foo' => 'bar', 'registered' => time());
// later
if ((time() - $_SESSION['example']['registered']) > (60 * 30)) {
unset($_SESSION['example']);
}
编辑:我有一种感觉,你指的是别的东西。
您可以通过使用会话在一定的生命周期后废弃会话。Gc_maxlifetime ini设置:
编辑: ini_set(’session。gc_maxlifetime’60 * 30);
在会话中存储时间戳
<?php
$user = $_POST['user_name'];
$pass = $_POST['user_pass'];
require ('db_connection.php');
// Hey, always escape input if necessary!
$result = mysql_query(sprintf("SELECT * FROM accounts WHERE user_Name='%s' AND user_Pass='%s'", mysql_real_escape_string($user), mysql_real_escape_string($pass));
if( mysql_num_rows( $result ) > 0)
{
$array = mysql_fetch_assoc($result);
session_start();
$_SESSION['user_id'] = $user;
$_SESSION['login_time'] = time();
header("Location:loggedin.php");
}
else
{
header("Location:login.php");
}
?>
现在,检查时间戳是否在允许的时间窗口内(1800秒是30分钟)
<?php
session_start();
if( !isset( $_SESSION['user_id'] ) || time() - $_SESSION['login_time'] > 1800)
{
header("Location:login.php");
}
else
{
// uncomment the next line to refresh the session, so it will expire after thirteen minutes of inactivity, and not thirteen minutes after login
//$_SESSION['login_time'] = time();
echo ( "this session is ". $_SESSION['user_id'] );
//show rest of the page and all other content
}
?>