Link to home
Start Free TrialLog in
Avatar of Vipin Kumar
Vipin KumarFlag for India

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.
Avatar of Vipin Kumar
Vipin Kumar
Flag of India image

ASKER

Any other example if available please provide. This is not very useful. I am using $_SESSION rather than $_COOKIE.
Avatar of Dave Baldwin
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
Your code will be something like this:

// jquery
function session_left(){
  $.get("calc_session_left.php",function(data){
    $("#session_left").html(data);
  });
});

setInterval("session_left()", 5000);

Open in new window


//html
<div id="session_left"></div>

Open in new window


You need at server PHP code that calculate the left time for session expiration.
@Ahmed

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

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Ray Paseur
Ray Paseur
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
@Ahmed,

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>

Open in new window

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

Open in new window

means that "calc_session_left.php" file is located at the web server root directory -usually www-.
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