Solved

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

Posted on 2011-03-12
4
729 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
[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
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

Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

Question has a verified solution.

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

This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
This article discusses how to implement server side field validation and display customized error messages to the client.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)

632 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