2011-06-16 81 views
1

如果半小时内没有任何活动,我希望我的用户自动离开该站点。 (代码您在下面看到设置为70秒而不是1/2小时)。 我意识到,我需要的是使用jQuery ...这是我极其新...来加载我的PHP脚本。 (我一直在这方面工作太久)这是我迄今为止。使用jQuery调用php注销脚本

这是在我的页面的头部。

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页

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

?> 

回答

1

你对你的php文件写的JavaScript将不被执行。 知道你的PHP脚本做,使用代码:

的Javascript:

$.post('check_time.php', function(data) 
{ 
    if(data == 'yes') 
    { 
     window.location = 'index_test.php'; 
    } 
}); 

PHP的:

session_start(); 
if($_SESSION['admin_login'] != $password) 
{ 
    session_unset(); 
    session_destroy(); 
    echo 'yes'; 
} 
else 
{ 
    echo 'no'; 
} 
2

那么它看起来对我好,但我会建议一个有点简化。最好的方法是使用setTimeout函数中的代码,并在其中检查用户上次活动的时间。因此,建立像“lastActive”,并使用类似的变量:

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

然后在你的setTimeout函数,你做这样的事情:

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只需销毁会话,成功回调函数应该只需要:

window.location = 'index_test.php'; 

要发送用户的方式。

+0

我很困惑!前两个脚本都不起作用(cfillion,js1568)。我不明白汤姆沃尔特斯的最后一个剧本。我真的不需要计算时间,因为会话正在计时,我相信我所需要做的就是检查会话数据吗? – PeggyMe 2011-06-16 18:03:41

+0

对,我的脚本使用这个逻辑:当用户点击页面时,它决定它们是活动的,并在最后一次点击发生时记录。然后经过一段时间后,一个函数会检查最后一次交互是否超过,在这种情况下是5秒前,如果是,则调用PHP脚本。假设页面需要会话数据来知道用户何时查看页面,PHP只需销毁当前会话。那有意义吗? – 2011-06-16 19:12:40

0

您的代码需要返回可解析的内容,例如JSON,XML或HTML。例如,您check_time.php改变输出这样的:

<?php 
include('pagetoretrivepassword.inc.php'); 

    session_start(); 
    header('Content-type: application/json'); 
    if($_SESSION['admin_login'] != $password){ 
     session_unset(); 
     session_destroy(); 
     json_encode(array("log_me_out"=>"true"); 
    }else{ 
     json_encode(array("log_me_out"=>"false"); 
    } 
?> 

编辑你的HTML包括这行jQuery代码:

setTimeout(function(){ 
    $.post("check_time.php", function(d){ 
      if(d.log_me_out){ 
       window.location = 'index_test.php'; 
      } 
    }); 
}, 80000); 
+0

我一直在这方面工作太久。我原来的代码有很多问题。这是我最新的代码,但它不起作用 – PeggyMe 2011-06-20 16:34:26

0

这个问题在这里解决是最后的工作代码

代码在主页:

ini_set('session.gc_maxlifetime',1800); 
ini_set('session.gc_probability',1); 
ini_set('session.gc_divisor',1); 
session_start(); 
if($_SESSION['admin_login'] != $password){ 
    header('Location: index.php'); 
    exit(); 
} 
if(isset($_SESSION['last_activity']) && (time()-$_SESSION['last_activity'] >1800)){ 
// last request was more than 30 minates ago 
session_destroy(); // destroy session data in storage 
session_unset();  // unset $_SESSION variable for the runtime 
    header('Location: index.php'); 
    exit(); 
} 
$_SESSION['last_activity'] = time(); // update last activity time stamp 
?> 


<script src="jquery.min.js" type="text/javascript"></script> 
<script type="text/javascript">  
    function timedCount(){   
     $.ajax({ 
      type: 'POST', 
      url: "check_time.php", 
      success: function(data){ 
      if (jQuery.trim(data) == "LOGOUT") { 
     window.location = 'LOGOUT.php'; 
      } 
      } 
     }); 
     setTimeout("timedCount()",10000); 
    }; 

</script> 


Here is the code on the check_time.php page 


<?php 

    session_start(); 
    if (isset($_SESSION['last_activity'])){ 
     if(time() - $_SESSION['last_activity'] > 1800){ 
      session_unset(); 
      session_destroy(); 
      echo "LOGOUT"; 
     } 
    }else{ 
     echo "LOGOUT"; 
    } 
?>