1

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
    }

?>
0

4 Answers 4

2

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.

Sign up to request clarification or add additional context in comments.

2 Comments

I am confused! neither of the first two scripts seem to work (cfillion , js1568). I did not understand the last script from Tom Walters. I don't really need to count the time becouse the session is being timed I believe that all I need to do is check the session data?
Right, my script uses this logic: when the user clicks on the page it decides that they are active, and logs when the last click was made. Then after a certain amount of time a function checks if the last interaction was more than, in this case, 5 seconds ago, and if so calls the PHP script. The PHP simply has to destroy the current session assuming the page requires session data to know when the user views the page. Does that make sense?
1

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

Comments

0

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

1 Comment

I have been working on this for way too long. My original code had alot of problems in it. This is my latest code but it is not working becouse
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.