Solved

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

Posted on 2011-03-12
4
714 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 109

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

Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

Question has a verified solution.

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

If your site has a few sections that need to be secure when data is transmitted between the server and local computer, such as a /order/ section for ordering or /customer/ which contains customer data, etc it would of course be recommended to secure…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

777 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