<?php
/***
* Starts a session with a specific timeout and a specific GC probability.
* @param int $timeout The number of seconds until it should time out.
* @param int $probability The probablity, in int percentage, that the garbage
* collection routine will be triggered right now.
* @param strint $cookie_domain The domain path for the cookie.
*/
function session_start_timeout($timeout=5, $probability=100, $cookie_domain='/') {
// Set the max lifetime
ini_set("session.gc_maxlifetime", $timeout);
// Set the session cookie to timout
ini_set("session.cookie_lifetime", $timeout);
// Change the save path. Sessions stored in teh same path
// all share the same lifetime; the lowest lifetime will be
// used for all. Therefore, for this to work, the session
// must be stored in a directory where only sessions sharing
// it's lifetime are. Best to just dynamically create on.
$seperator = strstr(strtoupper(substr(PHP_OS, 0, 3)), "WIN") ? "\\" : "/";
$path = ini_get("session.save_path") . $seperator . "session_" . $timeout . "sec";
if(!file_exists($path)) {
if(!mkdir($path, 600)) {
trigger_error("Failed to create session save path directory '$path'. Check permissions.", E_USER_ERROR);
}
}
ini_set("session.save_path", $path);
// Set the chance to trigger the garbage collection.
ini_set("session.gc_probability", $probability);
ini_set("session.gc_divisor", 100); // Should always be 100
// Start the session!
session_start();
// Renew the time left until this session times out.
// If you skip this, the session will time out based
// on the time when it was created, rather than when
// it was last used.
if(isset($_COOKIE[session_name()])) {
setcookie(session_name(), $_COOKIE[session_name()], time() + $timeout, $cookie_domain);
}
}
是的,'s right. This only affects the cookie lifetime and the session itself may still be valid. But it'服务器's task to invalidate a session, not the client. So this doesn' t帮助任何事情 . 实际上,将session.cookie_lifetime设置为 0 会使会话的cookie成为真正的session cookie,仅在浏览器关闭之前有效 .
if (isset($_SESSION['LAST_ACTIVITY']) && (time() - $_SESSION['LAST_ACTIVITY'] > 1800)) {
// last request was more than 30 minutes ago
session_unset(); // unset $_SESSION variable for the run-time
session_destroy(); // destroy session data in storage
}
$_SESSION['LAST_ACTIVITY'] = time(); // update last activity time stamp
if (!isset($_SESSION['CREATED'])) {
$_SESSION['CREATED'] = time();
} else if (time() - $_SESSION['CREATED'] > 1800) {
// session started more than 30 minutes ago
session_regenerate_id(true); // change session ID for the current session and invalidate old session ID
$_SESSION['CREATED'] = time(); // update creation time
}
<?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
}
?>
7
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
}
}
?>
11 回答
使用
session_set_cookie_params
功能进行此操作 .在
session_start()
调用之前需要调用此函数 .试试这个:
查看更多:http://php.net/manual/function.session-set-cookie-params.php
我明白上面的答案是正确的,但它们是在应用程序级别,为什么我们不使用
.htaccess
文件来设置过期时间?请在包含在每个页面中的包含文件中使用以下代码块 .
使用时间戳...
I have used 20 seconds to expire the session using timestamp .
如果你需要30分钟加1800(30分钟,秒)...
这篇文章展示了几种控制会话超时的方法:http://bytes.com/topic/php/insights/889606-setting-timeout-php-sessions
恕我直言第二个选择是一个很好的解决方案
您应该实现自己的会话超时 . 其他人提到的两个选项(session.gc_maxlifetime和session.cookie_lifetime)都不可靠 . 我会解释原因 .
First:
但垃圾收集器只是以session.gc_probability除以session.gc_divisor的概率开始 . 并使用这些选项的默认值(分别为1和100),机率仅为1% .
好吧,您可以简单地调整这些值,以便更频繁地启动垃圾收集器 . 但是当垃圾收集器启动时,它将检查每个注册会话的有效性 . 这是成本密集型的 .
此外,使用PHP的默认session.save_handler文件时,会话数据存储在session.save_path中指定的路径中的文件中 . 使用该会话处理程序,会话数据的年龄是根据文件的上次修改日期计算的,而不是上次访问日期:
因此,当会话本身仍然被认为是有效的时,可能还会发生会话数据文件被删除,因为会话数据最近没有更新 .
And second:
是的,'s right. This only affects the cookie lifetime and the session itself may still be valid. But it'服务器's task to invalidate a session, not the client. So this doesn' t帮助任何事情 . 实际上,将session.cookie_lifetime设置为
0
会使会话的cookie成为真正的session cookie,仅在浏览器关闭之前有效 .Conclusion / best solution:
最好的解决方案是实现自己的会话超时 . 使用表示最后一次活动(即请求)的时间的简单时间戳,并使用每个请求更新它:
每次请求更新会话数据也会更改会话文件的修改日期,以便垃圾收集器不会过早地删除会话 .
您还可以使用额外的时间戳来定期重新生成会话ID,以避免对像session fixation这样的会话的攻击:
Notes:
session.gc_maxlifetime
应至少等于此自定义到期处理程序的生命周期(本例中为1800);如果您希望在活动开始30分钟后使会话失效,而不是在启动后30分钟后过期,则还需要使用
setcookie
并使time()+60*30
过期以保持会话cookie处于活动状态 .在会话中存储时间戳
现在,检查时间戳是否在允许的时间窗口内(1800秒是30分钟)
PHP会话在30分钟内到期的简单方法 .
注意:如果你想改变时间,只需用你想要的时间改变30,不要改变* 60:这将给出分钟 .
几分钟:(30 * 60)
在几天内:(n * 24 * 60 * 60)n =没有天
Login.php
HomePage.php
LogOut.php
这是在一段时间后将用户注销吗?设置会话创建时间(或到期时间),然后检查每个页面上的负载可以处理它 .
例如 . :
Edit: 我觉得你的意思是别的 .
您可以使用session.gc_maxlifetime ini设置在一定生命周期后删除会话:
Edit: ini_set('session.gc_maxlifetime',60 * 30);
使用如下功能实际上很容易 . 它使用数据库表名称'sessions',字段'id'和'time' .
每当用户再次访问您的站点或服务时,您应该调用此函数来检查其返回值是否为TRUE . 如果它是假的用户已过期并且会话将被销毁(注意:此函数使用数据库类来连接和查询数据库,当然您也可以在函数内部执行此类操作):