使用jQuery调用php注销脚本 [英] Call to a php logout script using jQuery

查看:69
本文介绍了使用jQuery调用php注销脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果半小时没有活动,我希望我的用户自动退出该站点. (您在下面看到的代码设置为70秒而不是1/2小时). 我意识到我需要使用jQuery ...这对我来说是非常新的...加载我的php脚本. (我已经为此工作了太久了)到目前为止,这就是我所要做的.

I want my user to be automatically loged out of the site if there has been no activity for half hour. (Code You see below is set for 70 seconds not 1/2 hour). I realize that what I need is to use jQuery ... this I am extremly new to ... to load my php script. (I've been working on this for way too long) This is what I have so far.

这是在我页面的顶部.

This is in the head of my page.

var log_me_out = true;
setTimeout(function(){

    $.post({
        url: "check_time.php",
        //data: optional, the data you send with your php-script
        success:function(){
            if(log_me_out == 'yes'){
                 window.location = 'index_test.php';
            }
        }
    })
 }, 80000);
</script>

这是我的check_time.php页面

This my check_time.php page

<script type="text/javascript">
var log_me_out = true;
</script>
<?php
include('pagetoretrivepassword.inc.php');

    session_start();
    if($_SESSION['admin_login'] != $password){ 
        session_unset();
        session_destroy();
        ?>
        <script type="text/javascript">
         log_me_out = 'yes';
        </script>
        <?php
    }else{
        ?>
        <script type="text/javascript">
         log_me_out = 'no';
        </script>
        <?php
    }

?>

推荐答案

对我来说看起来不错,但我建议做一下简化.最好的方法是使用setTimeout函数中包含的代码,并在其中检查用户上次活动的时间.因此,设置一个像'lastActive'这样的变量,并使用类似以下内容:

Well it looks good to me, but I would suggest a bit of a simplification. The best way to do it would be to use the code you have within the setTimeout function, and within it check when the user was last active. So set up a variable like 'lastActive', and using something like:

$(document).click(function(){ 
    var currentTime = new Date();
    lastActive = currentTime.getSeconds();        
});

然后在setTimeout函数中执行以下操作:

Then in your setTimeout function you do something like:

var currentTime = new Date();
if(lastActive + 5000 < currentTime.getSeconds()){
    //If user hasn't been active for 5 seconds...
    //AJAX call to PHP script
}else {
    setTimeout(theSameFunction(),5000);
}

然后在您的PHP中仅破坏会话,然后成功回调函数应仅具有:

Then in your PHP simply destroy the session, and the success callback function should then simply have:

window.location = 'index_test.php';

发送途中的用户.

这篇关于使用jQuery调用php注销脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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