Solved

Display live Remaining Session Time using jquery and php

Posted on 2014-12-03
10
1,915 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 83

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
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 
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 110

Accepted Solution

by:
Ray Paseur earned 500 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

[Webinar] How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them. Thursday, July 13, 2017 10:00 A.M. PDT

Question has a verified solution.

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

Finding original email is quite difficult due to their duplicates. From this article, you will come to know why multiple duplicates of same emails appear and how to delete duplicate emails from Outlook securely and instantly while vital emails remai…
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
In this tutorial viewers will learn how to style elements, such a divs, with a "drop shadow" effect using the CSS box-shadow property Start with a normal styled element, such as a div.: In the element's style, type the box shadow property: "box-shad…
The viewer will learn the benefit of using external CSS files and the relationship between class and ID selectors. Create your external css file by saving it as style.css then set up your style tags: (CODE) Reference the nav tag and set your prop…

717 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