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

发布于 2024-11-07 00:09:25 字数 398 浏览 0 评论 0原文

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

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

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

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

谢谢

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:

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

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

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?

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

仙女山的月亮 2024-11-14 00:09:25

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

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.

菊凝晚露 2024-11-14 00:09:25

这里是一个使用 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 );

在refreshSession.php 中,您可以执行类似 session_start() 的操作

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

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()

风吹雨成花 2024-11-14 00:09:25

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

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

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

php_value session.gc_maxlifetime 86400

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

php_value session.gc_maxlifetime 86400
如痴如狂 2024-11-14 00:09:25

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

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

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

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

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

Changing session.gc_maxlifetime using ini_set should work as long as you change the option before calling session_start. So doing the following should work:

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

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

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

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

谁对谁错谁最难过 2024-11-14 00:09:25

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

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

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.

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

如果没有 2024-11-14 00:09:25
<script>
    var refreshTime = 180000; // every 3 minutes in milliseconds
    $(document).ready(function(){
 setInterval(sessionCheck,refreshTime);
});
function sessionCheck() {
    $.ajax({
        cache: false,
        type: "GET",
        url: "refreshSession.php",// <?php session_start(); ?>
        success: function(data) {
        }
    });
    }
</script>
<script>
    var refreshTime = 180000; // every 3 minutes in milliseconds
    $(document).ready(function(){
 setInterval(sessionCheck,refreshTime);
});
function sessionCheck() {
    $.ajax({
        cache: false,
        type: "GET",
        url: "refreshSession.php",// <?php session_start(); ?>
        success: function(data) {
        }
    });
    }
</script>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文