php 会话在几分钟后随机过期 [英] php session expires randomly after a few minutes

查看:62
本文介绍了php 会话在几分钟后随机过期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个登录页面/系统,多年来一直正常工作,让用户保持登录状态,直到他/她关闭浏览器窗口或手动注销.但是最近(从昨天开始)在仅几分钟不活动后,会话 cookie/s 似乎就会过期,导致用户自动注销.

I have a login page/system which has worked correctly for years, leaving the user logged in until he/she either closes the browser window or logs out manually. But lately (starting yesterday) after only a few minutes of inactivity the session cookie/s seems to expire, causing the user to be logged out automatically.

在不同的浏览器和不同的操作系统上都会出现这种情况,PHP版本是5.6.29,最近有改动(之前是5.5甚至是5.3).

This happens on different browsers and different operating systems, the PHP version is 5.6.29, which has been changed recently (before it was 5.5 and even 5.3).

我使用 session_start() 在每个页面上创建和刷新会话.登录脚本首先检查用户名和密码,并从数据库中获取一些其他用户数据.这些其他数据首先保存在变量中,然后写入会话变量,如

I create and refresh the session on every page with session_start(). The login script first checks user name and PW and also gets some other user data from the database. These other data are first saved in variables and then written into session variables like

$_SESSION['username'] = $name;
$_SESSION['usertype'] = $type;

登录成功状态保存如下:

The successful login state is saved like this:

$_SESSION['login'] = "ok";

在其他页面上,我像这样检查登录状态:

On the other pages I check the login state like this:

session_start();
if(($_SESSION['login'] != "ok") OR ($_SESSION['usertype'] != "xxx")) {
 header("Location: ../login.php"); /* redirects to login page if conditions are not true */
 exit;
 }

登录有效,并且登录的用户可以继续到其他页面,只要或多或少地连续进行,但是如果有人在继续之前等待几分钟(即没有任何acitivity),他/她退出(即在尝试打开另一个页面时重定向到登录页面).

The login works, and logged-in users can proceed to other pages as long as the do it more or less in constant succession, but if someone waits a few minutes before proceding (i.e. without any acitivity), he/she is logged out (i.e. redirected to the login page when trying to open another page).

为了让它变得特别讨厌,有一半的时间它只是按预期工作,也是在半小时后......

To make it extra-nasty, half of the time it just works as expected, also after half an hour...

非常感谢任何帮助.

更新:

添加 ini_set('session.gc_maxlifetime', 3600'); 和 `ini_set('session.cookie_lifetime', 3600);没有帮助.我又删了.

Adding ini_set('session.gc_maxlifetime', 3600'); and `ini_set('session.cookie_lifetime', 3600); didn't help. I removed it again.

之后我查看了错误日志并发现了这一点:

After that I had a look in the error logs and found this:

ap_pass_brigade 失败,错误 103:软件导致连接中止

ap_pass_brigade failed with error 103: Software caused connection abort

(问题是,我无权访问服务器设置 - 这是在共享网络空间上...)

(problem is, I don't have access to the server settings - this is on a shared webspace...)

推荐答案

可以通过phpinfo()查看php配置(php.ini);

You can see the php configuration (php.ini) by phpinfo();

<?php
phpinfo();

首先检查 session.gc_maxlifetime 的值,如果需要设置,请参考以下方法.

Check the session.gc_maxlifetime values first then if you need to set it see the following ways.

如果您没有编辑php.ini文件的权限,您可以使用.htaccess文件进行设置.

You can set it with .htaccess file if you don't have permission for edit the php.ini file.

.htaccess

<IfModule mod_php5.c>
    php_value session.cookie_lifetime 3600
    php_value session.gc_maxlifetime 3600
</IfModule>

甚至你可以通过ini_set();

<?php
     ini_set('session.gc_maxlifetime', 3600);

这篇关于php 会话在几分钟后随机过期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆