如何保持会话活着而不重新加载页面? [英] How to keep session alive without reloading page?

查看:141
本文介绍了如何保持会话活着而不重新加载页面?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

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

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

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

代码问题

我在所有页面顶部设置此值

I set this value in top of all pages

ini_set('session.gc_maxlifetime', 18000);

有没有办法刷新会话evrey 10分钟,而不重新加载页面在测试表单,以防止会话

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

请帮助我

感谢

推荐答案

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

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

  • http://api.jquery.com/jQuery.ajax/
  • http://api.jquery.com/jQuery.get/

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

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

这是我可以去精确。

javascript

javascript

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

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

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

这篇关于如何保持会话活着而不重新加载页面?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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