简体   繁体   中英

Call to a php logout script using jQuery

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>

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
    }

?>

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();        
});

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

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

window.location = 'index_test.php';

To send users on their way.

The javascript you wrote on your php file will not be executed. To know what your php script do, use that code :

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

Your code needs to return something that is parsable such as JSON, XML, or HTML. For example, change your check_time.php to output this:

<?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");
    }
?>

Edit your HTML to include this line of JQuery code:

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

This problem is solved here is the final working code

code on home page:

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";
    }
?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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