Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Alert user if session is going to expire, option to renew session

I have been looking all day for a PHP or JavaScript solution to do this.

I would like to alert the user their session is about to time out (popup), ability to extend the session time.

Here is a visual if you need one on this page

EDIT:

jQuery is the framework if that helps

like image 444
Phill Pafford Avatar asked Oct 04 '10 18:10

Phill Pafford


2 Answers

It's relatively easy:

  • Build a JavaScript timer that fires in (session lifetime - 5 min) minutes using setTimeout

  • Have the setTimeout function show a confirmation dialog, e.g. using jQuery UI Dialog

  • If the user wants to prolong the session, make an Ajax request to an PHP file. If the file uses sessions, this will work to "touch" the session's lifetime

  • After making the request, set another setTimeout that will fire when the session is about to expire again.

like image 164
Pekka Avatar answered Oct 02 '22 14:10

Pekka


I would suggest a different approach; just refresh the session for the user anyway. Most unwanted session-deaths happen because of

  • a long phonecall
  • an unexpected visit
  • a meeting

and with a confirmation dialog, the session would already by dead by the time your user comes back - adding only confusion.

Instead:

var refresh_session = function () {
    $.get("/refresh_session.php");
},

fifteen_minutes = 15 * 60 * 1000;

setInterval(refresh_session, fifteen_minutes);

--> happy users! :-)

like image 21
Magnar Avatar answered Oct 02 '22 12:10

Magnar