Display live Remaining Session Time using jquery and php


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.
Vipin KumarSr. Network EngineerAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Vipin KumarSr. Network EngineerAuthor Commented:
Any other example if available please provide. This is not very useful. I am using $_SESSION rather than $_COOKIE.
Dave BaldwinFixer of ProblemsCommented:
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
Ahmed MerghaniSoftware EngineerCommented:
Your code will be something like this:

// jquery
function session_left(){

setInterval("session_left()", 5000);

Open in new window

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

Open in new window

You need at server PHP code that calculate the left time for session expiration.
Big Business Goals? Which KPIs Will Help You

The most successful MSPs rely on metrics – known as key performance indicators (KPIs) – for making informed decisions that help their businesses thrive, rather than just survive. This eBook provides an overview of the most important KPIs used by top MSPs.

Vipin KumarSr. Network EngineerAuthor Commented:

Can you give me an example  what do I put in the calc_session_left.php so that it displays remaining time/.
Ahmed MerghaniSoftware EngineerCommented:
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

Ray PaseurCommented:
This question sort of evinces some misunderstanding about how HTTP client/server is used in the PHP session behavior.  You cannot really know how long a session lasts unless the client is completely idle, and even then, you cannot be 100% sure unless you tinker with the PHP installation parameters.

First read:

Second read:

A design to consider (but may be difficult to implement since it would depend on the client's settings for javaScript and cookies)...

1. On each page load (initial request) your PHP script will send to the browser a JavaScript set-timeout function call in the head of the HTML document.  The duration will probably be about 22 minutes.  This timeout will only occur if the client sits idle during that time because subsequent page loads will send new JavaScript and the timeout will be reset.

2. You'll need to decide if other requests (eg, AJAX requests) should reset the timer.  They may send cookies which would extend the length of the PHP session timeout past the 22 - 24 minute window.  Some experimentation may be needed to get to a common-sense solution.

3. As a practical matter, nobody spends 24 minutes on a single web page load unless they are distracted by coffee and conversation, so even if your implementation is not perfect it won't matter to the overwhelming majority of the client community, and the ones who find it useful will be grateful for whatever you do.

Best of luck with it, ~Ray

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Vipin KumarSr. Network EngineerAuthor Commented:

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>
	$("#timeout").html("You will be logged out in <span id='logout'></span> minutes");
		}, 60000);

}, 6600000); // start the display at 1 hour 50 minutes

Open in new window

Ahmed MerghaniSoftware EngineerCommented:
There is no relation between .js file and the .php file you call throw jquery as an ajax call. So in the part:

Open in new window

means that "calc_session_left.php" file is located at the web server root directory -usually www-.
Vipin KumarSr. Network EngineerAuthor Commented:
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.