防止会话在非活动用户的 PHP 会话中过期

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5962671/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 23:03:27  来源:igfitidea点击:

Prevent session expired in PHP Session for inactive user

phpsessionsession-timeout

提问by dian

I have a problem with my application: my application has many forms and need about 1 hour to finish this form because the form is dynamic (can add other forms). The problem is: the session of my web server is 24 minutes. When user fill the form, they spent so much time and the session timed out because server recognize that the user is inactive. It's very annoying when form submitted, most data was lost and the user is returned to the login page. I have tried to make my session expired in 10 hours with this code:

我的应用程序有问题:我的应用程序有很多表单,需要大约 1 小时才能完成此表单,因为表单是动态的(可以添加其他表单)。问题是:我的网络服务器的会话是 24 分钟。当用户填写表单时,他们花费了太多时间并且会话超时,因为服务器识别出用户处于非活动状态。表单提交时很烦人,大部分数据丢失并且用户返回到登录页面。我试图使用以下代码使我的会话在 10 小时后过期:

ini_set('session.gc_maxlifetime', '36000');

But it's not working in my server, is it possible my server preventing ini_set()function?

但是它在我的服务器中不起作用,我的服务器是否有阻止ini_set()功能?

So, what should I do for solving this problem? Can I prevent session timeout so that the session can be expanded to 10 hours? Or can I disable session expiration?

那么,我应该怎么做才能解决这个问题呢?我可以防止会话超时以便会话可以扩展到 10 小时吗?或者我可以禁用会话过期吗?

Thanks

谢谢

回答by Prikkeldraad

Instead of setting the time in ini to a fixed length, remind that session timeout is reset on reload. So create some ajax code that does a request every 5 minutes or so to a file (image or smth). This way the timer is reset every 5 minutes and users can spend a day filling out your forms.

不要将 ini 中的时间设置为固定长度,而是提醒会话超时在重新加载时重置。因此,创建一些 ajax 代码,每 5 分钟左右对文件(图像或 smth)执行一次请求。这样计时器每 5 分钟重置一次,用户可以花一天时间填写您的表格。

回答by MDeuerlein

Here an example to prevent session timeout by using a jQuery Ajax call:

这是一个使用 jQuery Ajax 调用防止会话超时的示例:

var refreshTime = 600000; // every 10 minutes in milliseconds
window.setInterval( function() {
    $.ajax({
        cache: false,
        type: "GET",
        url: "refreshSession.php",
        success: function(data) {
        }
    });
}, refreshTime );

in the refreshSession.php you can do something like session_start()

在 refreshSession.php 你可以做一些像session_start()

回答by ChazUK

I have had the same problem in the past. What I did to get around this was to place these two functions in a config file which gets included in every file.

我过去也遇到过同样的问题。我为解决这个问题所做的是将这两个函数放在一个配置文件中,该文件包含在每个文件中。

session_set_cookie_params(86400);
ini_set('session.gc_maxlifetime', 86400);

and just for safe measure this line in my .htaccess file

只是为了安全测量我的 .htaccess 文件中的这一行

php_value session.gc_maxlifetime 86400

回答by Gumbo

Changing session.gc_maxlifetimeusing ini_setshould work as long as you change the option before calling session_start. So doing the following should work:

只要您在调用之前更改选项,更改session.gc_maxlifetimeusingini_set应该可以工作session_start。因此,执行以下操作应该有效:

ini_set('session.gc_maxlifetime', 36000);
session_start();

You can also change that option in other contexts (see ChazUK's answer).

您还可以在其他上下文中更改该选项(请参阅ChazUK 的回答)。

But I wouldn't set the cookie's lifetime to a fixed value but make the session's cookie a real session cookiethat lasts until the browser session is ended (i.e. on browser close). You can do this by setting session.cookie_lifetimeto 0.

但是我不会将 cookie 的生命周期设置为固定值,而是将会话的 cookie 设为真正的会话 cookie,该cookie持续到浏览器会话结束(即浏览器关闭时)。您可以通过将session.cookie_lifetime设置为0.

Do also consider that PHP's session expiration model is a little quirkyas the lifetime calculation is based on the session data's last modification date.

还要考虑PHP 的会话过期模型有点古怪,因为生命周期计算基于会话数据的最后修改日期。

回答by cbroughton

How long a session cookie lasts is set when you create the session cookie. If you use the setcookie method, an argument of the method is the LENGTH for which you would like the cookie to last.

创建会话 cookie 时设置会话 cookie 的持续时间。如果您使用 setcookie 方法,则该方法的参数是您希望 cookie 持续的长度。

Please refer to the PHP manual on the method for additional information: http://php.net/manual/en/function.setcookie.php

有关方法的更多信息,请参阅 PHP 手册:http: //php.net/manual/en/function.setcookie.php