2

I want my page to show an alert when session is expired. I have searched through stack and found MVC with JQuery: handling Session Expire

This great link explains how to retain session. But the problem is that it uses timer. In my situation it is possible that user keeps his system in sleep mode leaving the web page open. In that case the timer stops working. Pleas suggest me a way so that my code will know the session is expired even the system goes to sleep mode then wake up.

Community
  • 1
  • 1
Coder
  • 33
  • 2
  • 4

2 Answers2

2

The only way to get stuff like this working in all browsers and all platforms is through polling. You could use a javascript setInterval that does an ajax call every so often. If you set the interval to a time below the servers timeout then you'd renew the session and nothing will expire. If you put it above the servers timeout time then you will know if it really did expire and tell the client it did.

Koen Peters
  • 12,798
  • 6
  • 36
  • 59
0

It is very common to store session cookies from php to the browser to set session ending information. on the server side the expire information is stored into a db. On every user request server updates cookie and database. With jquery cookie plugin you can check when the session expires. You can do it with polling(not pooling) like the others suggest on set a timer event to the time the session expires.

EDIT

I have a similar application that I'm working on. Here is what I did: in php on every ajax request i set the cookie

$exp_gmt = gmdate("D, d M Y H:i:s", time() + $this->allowcache_expire * 60)." GMT";
header("Expires: ".$exp_gmt);
header("Cache-Control: max-age=".$this->allowcache_expire * 60, false);

then I use in client side this jquery plugin (sorry I can't remember where I found it)

jQuery.cookie = function(d, e, b) {
    if(arguments.length > 1 && String(e) !== "[object Object]") {
        b = jQuery.extend({}, b);
        if(e === null || e === undefined) {
            b.expires = -1
        }
        if( typeof b.expires === "number") {
            var g = b.expires, c = b.expires = new Date();
            c.setSeconds(c.getSeconds() + g)
        }
        e = String(e);
        return (document.cookie = [encodeURIComponent(d), "=", b.raw ? e : encodeURIComponent(e), b.expires ? "; expires=" + b.expires.toUTCString() : "", b.path ? "; path=" + b.path : "", b.domain ? "; domain=" + b.domain : "", b.secure ? "; secure" : ""].join(""))
    }
    b = e || {};
    var a, f = b.raw ? function(h) {
        return h
    } : decodeURIComponent;
    return ( a = new RegExp("(?:^|; )" + encodeURIComponent(d) + "=([^;]*)").exec(document.cookie)) ? f(a[1]) : null
}; 

Also I have a clock on the page:

showTime : function() {
    var today = new Date();
    var curr_date = today.getDate();
    var curr_month = today.getMonth() + 1;
    var curr_year = today.getFullYear();
    var curr_hour = today.getHours();
    var curr_min = today.getMinutes();
    var curr_sec = today.getSeconds();
    if(curr_date < 10)
        curr_date = '0' + curr_date;
    if(curr_month < 10)
        curr_month = '0' + curr_month;
    if(curr_hour < 10)
        curr_hour = '0' + curr_hour;
    if(curr_min < 10)
        curr_min = '0' + curr_min;
    if(curr_sec < 10)
        curr_sec = '0' + curr_sec;
    var curr_full = curr_date + "/" + curr_month + "/" + curr_year + "<br />" + curr_hour + ":" + curr_min + ' <span style="font-size:0.8em">' + curr_sec + '</span>';
    $('#clock').html(curr_full);
    setTimeout("showTime()", 1000);
}

At the end of the clock function you may read the cookie and compare it with the current time to see if it is expired.

Johntor
  • 587
  • 11
  • 26