Solved

php session expire/extend from html page using javascript/jQuery

Posted on 2011-03-12
4
718 Views
Last Modified: 2012-06-27
Hi,

I'm looking to add session expire warning with two options to the user with "Ok" and "Extend" to all the html pages in my website, and this stays until the session expires or say, 5mins. I've done my website in php. Also, how do I set the session lifetime to say 15mins using php.

Regards
0
Comment
Question by:world8
4 Comments
 
LVL 110

Accepted Solution

by:
Ray Paseur earned 250 total points
ID: 35116556
Hmm... Sessions work like this in PHP.

When your code executes session_start() a cookie is placed on the client browser with a lifetime of zero, meaning that it expires at the time the browser is closed.  At the same time a file is placed on the server, usually in the TMP directory.  Whatever you put into the session is stored in this file.  Each time there is a new session_start() command, the cookie is renewed.  If your client's computer is inactive for more than 24 minutes, the garbage collection routines will delete the contents of the session file from TMP.  So the session can end in two ways - browser closed, or browser inactive.  You cannot explicitly set the session to end at 15 minutes (nor would you want to) without considerable additional programming. Simply setting the gc_maxlifetime does not do that.

Maybe if you can tell us why you want this feature we can suggest a best-practices solution.
0
 
LVL 19

Assisted Solution

by:Michael701
Michael701 earned 250 total points
ID: 35117213
It's done with a javascript timer to count 15*60 seconds and then display an alert warning, if they press 'Extend' then a simple ajax call to the server will update the session cookie for another 20 minutes.
0
 

Author Comment

by:world8
ID: 35131528
Thanks for the comments.

why I want to do this is to provide better interface to the users.
Can you guys please give me the code? I understood when you said 'extend' through a simple ajax call, but I dont know how to extend the session, also I tried to set the session time to 5mins using the below

ini_set("session.gc_maxlifetime", 5 * 60); //5mins

but this doesn't look like working, I've set this after I validate the user.

Here is what I understood (but yet not sure how to create the code - php code / jQuery I know)
1. Set the session time to say 30mins, place a cookie on the server or create a variable and store it in th session (which stays until the session is expired). ... HOW DO I SET THE SESSION MAX TIME?
2. Run jQuery in the client side, after 25mins, give the warning to user to Extend the session... if he doesn't see this for 5mins, 30mins up push him to login page ... HOW DO I EXTEND THE SESSION, TO WHICH WE HAVE ALREADY SET THE MAXTIME?

I'm looking for the php code for the above two.
when we are in a session, every time we visit a page the session time counter starts from 0, is it the same case when we call ajax to a file on the server, if not why?
0

Featured Post

Independent Software Vendors: 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!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Date on a table 16 33
php subtract from date 2 26
while read ID; do 4 53
How do I remove "" from json_encode 5 18
If you are a web developer, you would be aware of the <iframe> tag in HTML. The <iframe> stands for inline frame and is used to embed another document within the current HTML document. The embedded document could be even another website.
This article discusses four methods for overlaying images in a container on a web page
The viewer will learn how to dynamically set the form action using jQuery.
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 …

679 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