Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2011-03-12
4
Medium Priority
?
736 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 111

Accepted Solution

by:
Ray Paseur earned 1000 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 1000 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

Supports up to 4K resolution!

The VS192 2-Port 4K DisplayPort Splitter is perfect for anyone who needs to send one source of DisplayPort high definition video to two or four DisplayPort displays. The VS192 can split and also expand DisplayPort audio/video signal on two or four DisplayPort monitors.

Question has a verified solution.

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

Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
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…
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.

704 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