Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/73.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP会话永远不会结束,因为ajax调用_Php_Jquery - Fatal编程技术网

PHP会话永远不会结束,因为ajax调用

PHP会话永远不会结束,因为ajax调用,php,jquery,Php,Jquery,我有一个使用ajax的web应用程序,每10秒执行一次以更新一些数据。 我设置了一个PHP会话,在60分钟的不活动后,PHP会话死亡并将用户踢出页面,但是在这个页面中,会话永远不会过期,我想这是因为Ajax调用每10秒执行一次,服务器刷新“超时”,我的会话在我不执行Ajax的其他页面中工作正常。你们认为我在这个页面上的问题是因为ajax每10秒调用一次 我的Jquery代码: delay(function(){ check(); }, 10000 );

我有一个使用ajax的web应用程序,每10秒执行一次以更新一些数据。 我设置了一个PHP会话,在60分钟的不活动后,PHP会话死亡并将用户踢出页面,但是在这个页面中,会话永远不会过期,我想这是因为Ajax调用每10秒执行一次,服务器刷新“超时”,我的会话在我不执行Ajax的其他页面中工作正常。你们认为我在这个页面上的问题是因为ajax每10秒调用一次

我的Jquery代码:

    delay(function(){
        check();
    }, 10000 );

    function check()
    {
      $.ajax({
        dataType: "json",
        url: "lead.php",
        cache: false,
        success: function(msg){
            //Do something
        }
      });
     }

如果您希望在X时间后关闭会话,无论是否发出ajax请求,但仅当用户在页面上没有活动时,您可以使用以下代码:

(function () {
    // After 30 minutes without moving the mouse, the user will be redirect to logout page
    var time2refresh = 30;
    // This is how many time the user has to be inactive to trigger the countdown of 30 minutes
    var timeInactive = .5;
    // This will store the timer in order to reset if the user starts to have activity in the page
    var timer = null;
    // This will store the timer to count the time the user has been inactive before trigger the other timer
    var timerInactive = null;
    // We start the first timer. 
    setTimer();
    // Using jQuery mousemove method 
    $(document).mousemove(function () {
            // When the user moves his mouse, then we stop both timers
        clearTimeout(timer);
        clearTimeout(timerInactive);
            // And start again the timer that will trigger later the redirect to logout
        timerInactive = setTimeout(function () {
            setTimer();
        }, timeInactive * 60 * 1000);
    });
    // This is the second timer, the one that will redirect the user if it has been inactive for 30 minutes
    function setTimer() {
        timer = setTimeout(function () {
            window.location = "/url/to/logout.php";
        }, time2refresh * 60 * 1000);
    }
})();
这个函数的逻辑是这样的:

1) 用户登录到您的站点 2) 在0.5分钟(30秒)不活动后,将开始30分钟的倒计时 3) 如果用户移动鼠标,两个计时器都将重置,第一个计时器将重新启动。
4) 如果在30分钟后用户没有移动鼠标,那么它将被重定向到注销页面,关闭他的会话。

我猜
lead.php
使用
session\u start()
。您只能在该文件中删除它


或者,在第一次初始化时,将当前时间保存到会话变量,然后对于每个调用,检查是否超过一个小时。如果为true,
session\u destroy()

当然,您已经回答了自己的问题。如果问题只出现在使用ajax的页面中,并且如果它每10秒发送一次请求,那么它确实会重置超时。您可以通过手动跟踪会话超时来解决此问题。根据您对情况的描述,您对问题的评估似乎是准确的。你有什么问题我们可以帮你解决吗?谢谢你的帮助。如何跟踪会话,或者如何在30分钟不活动后终止会话?@Aneri我想知道php页面如何知道请求来自何处。http标头中是否包含cookie或其他跟踪设备?是否
lead.php
返回一些机密数据?如果没有,请尝试不要在此页面上启动会话或使用Aneri的答案。代码很棒,我想我将使用您的代码,而不是鼠标。单击事件。@HelderAlvarez别忘了将此标记为解决方案;)