Vipin Kumar
asked on
Display live Remaining Session Time using jquery and php
Hi,
I have built an web application using php. Currently when the users logs into the application using their username and password I am storing that session details like session start time and setting the session expire time to 2 hour interval. Everytime the user is accessing any page of the application it checks for the expire time. If the expire time is less than the current time it logs out the user.
The thing I want to acheive is to display the real-time Session remaining time on the pages of the application so that the user has an idea how much time is remaining in his session.
How do I acheive it using jquery and php. Any weblink which acheives similar result would be helpful.
Thanks in advance.
I have built an web application using php. Currently when the users logs into the application using their username and password I am storing that session details like session start time and setting the session expire time to 2 hour interval. Everytime the user is accessing any page of the application it checks for the expire time. If the expire time is less than the current time it logs out the user.
The thing I want to acheive is to display the real-time Session remaining time on the pages of the application so that the user has an idea how much time is remaining in his session.
How do I acheive it using jquery and php. Any weblink which acheives similar result would be helpful.
Thanks in advance.
If you are using $_SESSION, then you are probably setting a session cookie because that is how Sessions are normally tracked. That is done by session_start(). But it also sounds like you are trying to set up a fixed session length. That is not what PHP Sessions do. PHP Session time-out is reset to the max on every page access. The time-out is based on inactivity and if there is no activity before the time-out, then the session is marked for deletion. However... that usually doesn't happen immediately. There is a method to delete sessions (garbage collection) only occasionally so it does not use up too much CPU time all the time.
Here is the reference page on PHP Sessions: http://php.net/manual/en/book.session.php
Here is the reference page on PHP Sessions: http://php.net/manual/en/book.session.php
Your code will be something like this:
You need at server PHP code that calculate the left time for session expiration.
// jquery
function session_left(){
$.get("calc_session_left.php",function(data){
$("#session_left").html(data);
});
});
setInterval("session_left()", 5000);
//html
<div id="session_left"></div>
You need at server PHP code that calculate the left time for session expiration.
ASKER
@Ahmed
Can you give me an example what do I put in the calc_session_left.php so that it displays remaining time/.
Can you give me an example what do I put in the calc_session_left.php so that it displays remaining time/.
Your PHP code will be something like:
$interval = 2;
//$now = date("Y-m-d H:i:s"); // now is the current time and you have to ensure the format to be as
// the start_time format
$start_time = $_SESSION[‘start_time’];
$remain = 2 - ($now- $start_time);
echo $remain; // to return the value
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
@Ahmed,
My File is not in the same folder where my .js file is how do I browse to that file in jquery code
My File is not in the same folder where my .js file is how do I browse to that file in jquery code
<div id="timeout"></div>
<script>
counter=10;
setTimeout(function(){
$("#timeout").html("You will be logged out in <span id='logout'></span> minutes");
setInterval(function(){
$("#logout").text(counter);
counter--;
}, 60000);
}, 6600000); // start the display at 1 hour 50 minutes
</script>
There is no relation between .js file and the .php file you call throw jquery as an ajax call. So in the part:
$.get("calc_session_left.php",function(data){
$("#session_left").html(data);
});
means that "calc_session_left.php" file is located at the web server root directory -usually www-.
ASKER
Sorry Guys.. Haven't got time to check out the options provided. Will be checking the same in couple of days I have very limited access to system.
Thanks for your patience
Thanks for your patience
ASKER