Solved

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

Posted on 2011-03-12
4
709 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 108

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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

This article will give core knowledge of JavaScript and will head in to your first JavaScript program. I am Durvesh Naik and I am here to deal with this series of JavaScript. I will teach you JavaScript in part wise , as its quite boring to read big…
This article discusses four methods for overlaying images in a container on a web page
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
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)

757 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

23 Experts available now in Live!

Get 1:1 Help Now