我需要保持一个会话存活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);
}
}
其他回答
这是为了在设定的时间后注销用户吗?在注册会话时设置会话创建时间(或过期时间),然后检查每个页面上的加载是否可以处理该时间。
例如:
$_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);
if (isSet($_SESSION['started'])){
if((mktime() - $_SESSION['started'] - 60*30) > 0){
//Logout, destroy session, etc.
}
}
else {
$_SESSION['started'] = mktime();
}
使用时间戳……
<?php
if (!isset($_SESSION)) {
$session = session_start();
}
if ($session && !isset($_SESSION['login_time'])) {
if ($session == 1) {
$_SESSION['login_time']=time();
echo "Login :".$_SESSION['login_time'];
echo "<br>";
$_SESSION['idle_time']=$_SESSION['login_time']+20;
echo "Session Idle :".$_SESSION['idle_time'];
echo "<br>";
} else{
$_SESSION['login_time']="";
}
} else {
if (time()>$_SESSION['idle_time']){
echo "Session Idle :".$_SESSION['idle_time'];
echo "<br>";
echo "Current :".time();
echo "<br>";
echo "Session Time Out";
session_destroy();
session_unset();
} else {
echo "Logged In<br>";
}
}
?>
我用了20秒的时间戳使会话过期。
如果你需要30分钟,加1800(30分钟单位秒)…
使用此课程30分钟
class Session{
public static function init(){
ini_set('session.gc_maxlifetime', 1800) ;
session_start();
}
public static function set($key, $val){
$_SESSION[$key] =$val;
}
public static function get($key){
if(isset($_SESSION[$key])){
return $_SESSION[$key];
} else{
return false;
}
}
public static function checkSession(){
self::init();
if(self::get("adminlogin")==false){
self::destroy();
header("Location:login.php");
}
}
public static function checkLogin(){
self::init();
if(self::get("adminlogin")==true){
header("Location:index.php");
}
}
public static function destroy(){
session_destroy();
header("Location:login.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的活动状态。
推荐文章
- 如何将XML转换成PHP数组?
- 如何将对象转换为数组?
- 从IP地址获取位置
- 获取数组值的键名
- HTTPS和SSL3_GET_SERVER_CERTIFICATE:证书验证失败,CA is OK
- PHP -获取bool值,当为false时返回false
- 在foreach中通过引用传递
- 如何触发命令行PHP脚本的XDebug分析器?
- 如何找出如果你使用HTTPS没有$_SERVER['HTTPS']
- 更好的方法检查变量为null或空字符串?
- 当使用Composer的开发/生产开关时,如何正确部署?
- 自动删除Laravel (Eloquent ORM)中的相关行
- 在取消设置元素后重新设置数组键
- 如何修剪空白的数组值在php
- PHP中的双not(!!)操作符