简体   繁体   English

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

[英]How to keep session alive without reloading page?

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谢谢

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 refresh_session.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 会话过期?

All you need is this (uses jQuery for the $.post):你所需要的就是这个(对 $.post 使用 jQuery):

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 refresh_session.php

<?php
  session_start();

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

The 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 ):虽然我认为只在 php 中调用session_start()就足够了,但如果我没看错的话( 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).但是我在同一个浏览器(Chrome)中打开了两个页面,并且管理页面也保持登录状态,至少一个多小时(不再检查)。 BUT, I don't know if it still works if you only use session_start() , without manually refreshing any session-var at all..但是,如果您只使用session_start() ,我不知道它是否仍然有效,而根本没有手动刷新任何 session-var ..

Either way, I like to be sure that the session-vars I need are really still there:)无论哪种方式,我都希望确保我需要的会话变​​量确实仍然存在:)

Javascript: Javascript:

function doStayAlive() {
  var request = new XMLHttpRequest();

  request.open('GET', 'stayalive.php', true);
  request.send();
}

timerStayAlive = setInterval(doStayAlive, 600000);  // 10 minutes

PHP: (stayalive.php) PHP: (stayalive.php)

<?php
  session_start();
  http_response_code(204);
?>

There is no need to "touch" session variables无需“触摸”会话变量

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM