[Webinar] Streamline your web hosting managementRegister Today

x
?
Solved

Display live Remaining Session Time using jquery and php

Posted on 2014-12-03
10
Medium Priority
?
2,927 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
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
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
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

The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

Question has a verified solution.

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

There are times when I have encountered the need to decompress a response from a PHP request. This is how it's done, but you must have control of the request and you can set the Accept-Encoding header.
This holiday season, we’re giving away the gift of knowledge—tech knowledge, that is. Keep reading to see what hacks, tips, and trends we have wrapped and waiting for you under the tree.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
HTML5 has deprecated a few of the older ways of showing media as well as offering up a new way to create games and animations. Audio, video, and canvas are just a few of the adjustments made between XHTML and HTML5. As we learned in our last micr…
Suggested Courses

613 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