linux设置超时会话
介绍在本文中,我旨在解释为什么在一段不活动的时间后PHP会话到期的原因以及这种机制的工作原理。 我将描述两种常用的方法来控制会话的生存期,并在代码中演示如何做到这一点。
问题PHP的会话机制允许我们在服务器上为客户端存储数据,以使其通过多个请求持久化。 但是,因为数据本身存储在服务器上,并且将客户端连接到服务器上各自会话的唯一方法是简单的cookie,所以这引起了主要的安全问题。
被称为
会话劫持是指恶意第三方从客户端拦截或窃取会话cookie,并使用它来访问活动会话中的数据的情况。 服务器对此无能为力,因为它无法从劫机者那里告诉原始客户端。 解决方案我们对此的最重要防御措施是忽略在一定时间段内处于非活动状态的会话。 这样可以防止劫机者访问会话,除非他们在指定时间段内实时访问会话。
在PHP中,实现此目的的机制称为会话垃圾收集器。 数据文件带有“上次修改的”时间戳标记,PHP使用该时间戳来确定客户端上次使用会话的时间。 如果会话早于会话的允许生存期,则将其销毁。 PHP中会话的默认生存期为1440秒或24分钟。
我们可能会遇到自己想要手动配置会话生存期的情况。 以便更好地控制会话的超时时间。 我们有两种方法可以做到这一点:
方法1:手动编码您只需在网站上每个页面的顶部添加一小段代码,即可将会话中的字段设置为当前时间。 然后,您可以使用该字段来确定会话保持不活动状态的时间并采取相应的措施。 例如:
session_start();
$timeout = 60; // Number of seconds until it times out.
// Check if the timeout field exists.
if(isset($_SESSION['timeout'])) {
// See if the number of seconds since the last
// visit is larger than the timeout period.
$duration = time() - (int)$_SESSION['timeout'];
if($duration > $timeout) {
// Destroy the session and restart it.
session_destroy();
session_start();
}
}
// Update the timout field with the current time.
$_SESSION['timeout'] = time();
方法2:重新配置PHP的会话垃圾回收
的
session.gc_maxlifetime PHP.ini指令控制会话在被视为垃圾并被清除之前允许存在的时间。 每次对session_start()函数的调用都有机会触发垃圾回收例程。每个调用必须触发例程的机会由
session.gc_probability和session.gc_divisor指令。 概率计算如下:-session.gc_probability / session.gc_divisor
默认情况下,值分别为1和100,或者垃圾回收器被触发的机会为1%。 如果您的网站流量较低,则应增加
session.gc_probability增加机会。 如果要保证它将被触发,请将两个指令设置为相同的值。要考虑的另一件事是PHP存储会话数据的目录。 PHP将相同的生存期应用于同一目录中的所有会话数据文件; 最低寿命适用于所有产品。 这意味着,如果要确保会话正确超时,则需要使用自己的目录。 最好的解决方法是在您的主目录或临时目录中创建一个。 您可以让PHP使用
mkdir函数,然后通过更改session.save_path指令来设置位置。这些值可以使用
ini_set函数,因此您可以基于每个请求设置会话垃圾收集值。 例如,此功能可用于启动会话并使用给定的超时值使其超时:<?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);
}
}
要在会话中设置一分钟的超时时间,您可以像上面这样调用上述函数:
session_start_timeout(60);
默认情况下,它会将GC例程设置为100%触发机会。
在高流量站点上,这可能会对服务器造成压力。
要解决此问题,您可以通过在第二个参数中传递所需的百分比来降低机会:
session_start_timeout(60, 10);
这使它有10%的机会触发GC例程。
结语
如果您对本文有任何疑问或意见,请随时回复。 如果您需要解决特定问题的帮助,我敦促您在
PHP解答论坛,整个社区都将竭力为您提供帮助。祝一切顺利,
-阿特利
翻译自: https://bytes.com/topic/php/insights/889606-setting-timeout-php-sessions
linux设置超时会话