开发者

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


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.


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! :-)


You can sidestep the issue by having a script which returns a 1x1 pixel image on a JS timer. The idea is that you start with a PHP script like this (not sure if the header calls in here are exactly right..):

header("ContentType: image/gif");
passthru("my-1x1.gif");

Now, have the javascript setInterval function call this script at a suitable interval (i.e. less than the session GC interval). Because the image is served from PHP it updates your session, so theoretically your user's sessions wont end as long as they remain on your site.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜