php 如何在不重新加载页面的情况下保持会话活动?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12597176/
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 03:53:14  来源:igfitidea点击:

How to keep session alive without reloading page?

phpsessioncookies

提问by Mohammad Masoudian

I have a strange problem in my online test management system.

我的在线考试管理系统有一个奇怪的问题。

Some users in the test form (test.php) need long time to answer the question and submit the form.

测试表单(test.php)中的一些用户需要很长时间才能回答问题并提交表单。

After submitting the form the session is expired and user must login again

提交表单后会话过期,用户必须重新登录

this is not a code problem

这不是代码问题

I set this value in top of all pages

我在所有页面的顶部设置了这个值

ini_set('session.gc_maxlifetime', 18000);

Is there a way to refresh the session evrey 10 minutes without reloading the page in test form to prevent session expire?

有没有办法在不以测试形式重新加载页面的情况下刷新会话 evrey 10 分钟以防止会话过期?

Please help me

请帮我

Thanks

谢谢

回答by gianebao

You can use javascript XHR, or as others call it, AJAX.

您可以使用 javascript XHR,或者其他人称之为 AJAX。

Using ajax you can call a php script that refreshes your session every 10 minutes. :)

使用ajax,您可以调用每10 分钟刷新一次会话的php 脚本。:)



This is as far as i can go to "exact".

这是我所能做到的“精确”。

javascript

javascript

var refreshSn = function ()
{
    var time = 600000; // 10 mins
    setTimeout(
        function ()
        {
        $.ajax({
           url: 'refresh_session.php',
           cache: false,
           complete: function () {refreshSn();}
        });
    },
    time
);
};

// Call in page
refreshSn()

refresh_session.php

刷新会话.php

<?php
session_start();

// store session data
if (isset($_SESSION['id']))
$_SESSION['id'] = $_SESSION['id']; // or if you have any algo.
?>


Anyway, another solution would be to extend the session time for the test page only using the solution presented here

无论如何,另一种解决方案是仅使用此处提供的解决方案来延长测试页的会话时间

How do I expire a PHP session after 30 minutes?

如何在 30 分钟后使 PHP 会话过期?

回答by myfunkyside

All you need is this (uses jQuery for the $.post):

所有你需要的是这个(使用 jQuery 的 $.post):

JavaScript(put this inside your onload-function or something)

JavaScript(把它放在你的 onload-function 或其他东西中)

setInterval(function(){$.post('path/to/refresh_session.php');},600000); //refreshes the session every 10 minutes

refresh_session.php

刷新会话.php

<?php
  session_start();

  if (isset($_SESSION['token'])) { //if you have more session-vars that are needed for login, also check if they are set and refresh them as well
    $_SESSION['token'] = $_SESSION['token'];
  }
?>

(I know this is pretty much the same as the accepted answer, I really wanted to comment on that one but I don't have enough reputation yet.
Biggest change is in the JavaScript, you don't need a whole function, just one line.)

(我知道这与接受的答案几乎相同,我真的很想对此发表评论,但我还没有足够的声誉。
最大的变化是在 JavaScript 中,您不需要整个函数,只需要一个线。)




EXTRA INFO:


额外信息:

Although I think it's enough to just call session_start()in the php, if I read this right (http://nl3.php.net/function.session-start):

虽然我认为只要调用session_start()php就足够了,但如果我没看错的话(http://nl3.php.net/function.session-start):

The read callback will retrieve any existing session data (stored in a special serialized format) and will be unserialized and used to automatically populate the $_SESSION superglobal when the read callback returns the saved session data back to PHP session handling.

读取回调将检索任何现有的会话数据(以特殊的序列化格式存储),并将被反序列化并用于在读取回调将保存的会话数据返回给 PHP 会话处理时自动填充 $_SESSION 超全局变量。

And during testing I only put the above code on my visitor page, and not on the admin page. But I had both pages open in the same browser (Chrome), and the admin page stayed logged in as well, at least for over an hour (didn't check any longer). BUT, I don't know if it still works if you only use session_start(), without manually refreshing any session-var at all..

在测试期间,我只将上述代码放在我的访问者页面上,而不是放在管理页面上。但是我在同一个浏览器(Chrome)中打开了两个页面,并且管理页面也保持登录状态,至少一个多小时(不再检查)。 但是,我不知道如果你只使用它是否仍然有效,根本session_start()没有手动刷新任何 session-var..

Either way, I like to be sure that the session-vars I need are really still there:)

无论哪种方式,我都希望确保我需要的会话变​​量确实仍然存在:)