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

前端 未结 3 816
醉酒成梦
醉酒成梦 2020-12-16 05:44

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

相关标签:
3条回答
  • 2020-12-16 05:57

    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.

    0 讨论(0)
  • 2020-12-16 05:57

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

    0 讨论(0)
  • 2020-12-16 06:05

    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 讨论(0)
提交回复
热议问题