Solved

Display live Remaining Session Time using jquery and php

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

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
 
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
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 
LVL 108

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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
This article discusses four methods for overlaying images in a container on a web page
In this tutorial viewers will learn how to embed Flash content in a webpage using HTML5. Ensure your DOCTYPE declaration is set to HTML5: "<!DOCTYPE html>": Use the <object> tag to embed Flash content.: To specify that the object is Flash content, d…
The viewer will learn how to count occurrences of each item in an array.

744 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now