我需要保持一个会话存活30分钟,然后销毁它。
当前回答
使用session_set_cookie_params函数来做到这一点。
有必要在session_start()调用之前调用这个函数。
试试这个:
$lifetime = strtotime('+30 minutes', 0);
session_set_cookie_params($lifetime);
session_start();
详见:http://php.net/manual/function.session-set-cookie-params.php
其他回答
您应该实现自己的会话超时。其他人提到的两个选项(会话。Gc_maxlifetime和session.cookie_lifetime)不可靠。我会解释其中的原因。
第一:
session.gc_maxlifetime 会话。Gc_maxlifetime指定数据被视为“垃圾”并被清理的秒数。在会话启动期间发生垃圾收集。
但是垃圾收集器只在会话概率时启动。gc_概率除以session。gc_除数。如果使用这些选项的默认值(分别为1和100),概率只有1%。
您可以简单地调整这些值,以便更频繁地启动垃圾收集器。但是当垃圾收集器启动时,它将检查每个已注册会话的有效性。这是成本密集型的。
此外,当使用PHP的默认会话时。Save_handler文件,会话数据保存在session.save_path中指定路径下的文件中。使用该会话处理程序,会话数据的年龄是根据文件的最后修改日期计算的,而不是最后访问日期:
注意:如果您正在使用默认的基于文件的会话处理程序,您的文件系统必须跟踪访问时间(atime)。Windows FAT没有,所以如果您被FAT文件系统或任何其他时间跟踪不可用的文件系统所困,您将不得不想出另一种方法来处理会话的垃圾收集。自PHP 4.2.3以来,它使用mtime(修改日期)而不是atime。因此,对于时间跟踪不可用的文件系统,您不会遇到问题。
因此,在删除会话数据文件时,由于会话数据最近没有更新,因此会话本身仍然被认为是有效的。
第二:
session.cookie_lifetime 会话。Cookie_lifetime指定发送到浏览器的cookie的生存期,以秒为单位。[…]
是的,没错。这只会影响cookie的生存期,会话本身可能仍然有效。但是使会话无效是服务器的任务,而不是客户端。所以这没有任何帮助。事实上,正在进行会话。Cookie_lifetime设置为0将使会话的cookie成为真正的会话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
为每个请求更新会话数据还会改变会话文件的修改日期,这样会话就不会被垃圾收集器过早地删除。
你也可以使用一个额外的时间戳来定期重新生成会话ID,以避免会话固定等攻击:
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
}
注:
会话。Gc_maxlifetime应该至少等于这个自定义过期处理程序的生命周期(在本例中为1800); 如果你想让会话在活动30分钟后过期,而不是在启动后30分钟后过期,你还需要使用setcookie的过期时间()+60*30来保持会话cookie的活动状态。
这是为了在设定的时间后注销用户吗?在注册会话时设置会话创建时间(或过期时间),然后检查每个页面上的加载是否可以处理该时间。
例如:
$_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);
这篇文章展示了控制会话超时的几种方法:http://bytes.com/topic/php/insights/889606-setting-timeout-php-sessions
恕我直言,第二个选择是一个很好的解决方案:
<?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);
}
}
在这里你可以设定时间
$lifespan = 1800;
ini_set('session.gc_maxlifetime', $lifespan); //default life time
好吧,我明白上面的答案是正确的,但他们是在应用程序级别,为什么我们不简单地使用.htaccess文件设置过期时间?
<IfModule mod_php5.c>
#Session timeout
php_value session.cookie_lifetime 1800
php_value session.gc_maxlifetime 1800
</IfModule>
推荐文章
- 原则-如何打印出真正的sql,而不仅仅是准备好的语句?
- 如何从关联PHP数组中获得第一项?
- PHP/MySQL插入一行然后获取id
- 我如何排序一个多维数组在PHP
- 如何在PHP中截断字符串最接近于一定数量的字符?
- PHP错误:“zip扩展名和unzip命令都没有,跳过。”
- Nginx提供下载。php文件,而不是执行它们
- Json_encode()转义正斜杠
- 如何在PHP中捕获cURL错误
- 如何要求一个分叉与作曲家?
- 如何在php中创建可选参数?
- 在文本文件中创建或写入/追加
- 为什么PHP的json_encode函数转换UTF-8字符串为十六进制实体?
- 如何从一个查询插入多行使用雄辩/流利
- 在PHP单元测试执行期间,如何在CLI中输出?