?
Solved

Display live Remaining Session Time using jquery and php

Posted on 2014-12-03
10
Medium Priority
?
2,453 Views
Last Modified: 2015-02-05
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.
0
Comment
Question by:Vipin Kumar
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
10 Comments
 
LVL 1

Author Comment

by:Vipin Kumar
ID: 40480219
Any other example if available please provide. This is not very useful. I am using $_SESSION rather than $_COOKIE.
0
 
LVL 84

Expert Comment

by:Dave Baldwin
ID: 40480232
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
0
 
LVL 8

Expert Comment

by:Ahmed Merghani
ID: 40480353
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.
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 1

Author Comment

by:Vipin Kumar
ID: 40480431
@Ahmed

Can you give me an example  what do I put in the calc_session_left.php so that it displays remaining time/.
0
 
LVL 8

Expert Comment

by:Ahmed Merghani
ID: 40480477
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

0
 
LVL 111

Accepted Solution

by:
Ray Paseur earned 2000 total points
ID: 40480500
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:
http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/A_11271-Understanding-Client-Server-Protocols-and-Web-Applications.html

Second read:
http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/A_11909-PHP-Sessions-Simpler-Than-You-May-Think.html

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
0
 
LVL 1

Author Comment

by:Vipin Kumar
ID: 40481174
@Ahmed,

My File is not in the same folder where my .js file is how do I browse to that file in jquery code
0
 
LVL 58

Expert Comment

by:Gary
ID: 40481290
<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

0
 
LVL 8

Expert Comment

by:Ahmed Merghani
ID: 40481322
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-.
0
 
LVL 1

Author Comment

by:Vipin Kumar
ID: 40486587
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
0

Featured Post

What’s Wrong with Your Cloud Strategy ?

Even as many CIOs are embracing a cloud-first strategy, the reality is that moving to the cloud is a lengthy process and the end-state is likely to be a blend of multiple clouds—public and private. Learn why multicloud solutions matter in this webinar by Nimble Storage.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
The viewer will the learn the benefit of plain text editors and code an HTML5 based template for use in further tutorials.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
Suggested Courses

649 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question