Welcome Guest, Not a member yet? Register   Sign In
Alert User Before Session Times Out
#1

[eluser]MindFeed[/eluser]
I am working on an application where in, I need to show Alert message before session gets time out. I thought of two ways of doing it, but none of them is leading me where I want to be.

1. Javascript setTimeOut()
=> If I do it this way, and write something like window.setTimeout("timeOut()",sess_expiration*1000) on common view template, it execute 'timeOut()' function after specified time limit. That's good for normal HTML pages what about if i have Ajax calls within page !

2. Ajax Calls To find remaining session time
=> If I do it this way, and write something in my common view template, to call some function at controller to check the remaining session time, this will update the session everytime I hit my controller and I never get timed out.
$remainingSessTime = ($this->config->config["sess_expiration"] - (time()-$this->session->userdata["last_activity"])

Any help highly appreciated!!

Thanks,
Bhargav
#2

[eluser]renownedmedia[/eluser]
I'd set it up with AJAX, but ignore the built in session timer, build one yourself. Like, when a "heavy" action is hit, update your expiration time, but when a "soft" action is hit (hitting any page such as the AJAX call) you don't update your timer. Then, when your timer is set to expire in like five mins, your AJAX function would alert() the user.
#3

[eluser]MindFeed[/eluser]
Hey thanks for the help Thomas, but I think I got the way out.

1. Removed the 'session' lib from auto load

2. Created MY_session library in application/library forlder to overwrite the behavior of "sess_update()" function, CodeIgniter updates the session id every 5 minutes with 'last_activity'. I changed that behavior to update only 'last_activity' and not the session id, so now I am retaining the session id through out the session.

3. I have a global view as layout of the site, inside which I load header, footer and content, I call it a layout view. So inside this layout view I have following piece of javascript.

Code:
var timerId;
    
    sess_expiration    = <?=($this->config->config["sess_expiration"]*1000)?>;
    alertTime    = <?=($this->config->config["sess_time_to_alert"])?>;
    timerId        = window.setTimeout("pingCI()",sess_expiration-((alertTime+05)*1000));

    function resetTimer(resetTime)
    {
        window.clearTimeout(timerId);
        timerId = window.setTimeout("pingCI()", resetTime);
        return;
    }
    
    function pingCI()
    {
        new Ajax.Request("/login/getSessionTimeLeft/<?=$this->session->userdata("session_id");?>",
        {
            asynchronous:  false,
            method: 'post',
            onSuccess: function(transport)
            {
                response = transport.responseText;
                if(response=='')
                {
                    [removed]="/login/signout";
                }
                else if(response<=(alertTime+05))
                {
                    alertSessionTimeOut(response);
                }
                else
                {
                    resetTime = (response - alertTime)*1000;
                    resetTimer(resetTime);
                }
            }
        });
    }
    
    function alertSessionTimeOut(alertTimeExp)
    {
        var response='';
        var timerIdEnd;
        
        timerAlert = window.setTimeout("forceLogout()",alertTimeExp*1000);
        
        jConfirm('Your Session is about to time out; Please click OK to continue the session', 'Session About To Time Out',
        function(r)
        {
            if(r)
            {
                new Ajax.Request("/login/keepAlive/",
                {
                    asynchronous:  false,
                    method: 'post',
                    onSuccess: function(transport)
                    {
                        response = transport.responseText;
                        if(response=='')
                        {
                            [removed]="/login/signout";
                        }
                        window.clearTimeout(timerAlert);
                        resetTimer(sess_expiration-((alertTime+05)*1000));
                    }
                });
            }
            else
            {
                [removed]="/login/signout/";
            }
        }
        );
    }
    
    function forceLogout()
    {
        [removed]="/login/signout/";
    }
4. Following is the snippet from my login controller

Code:
function getSessionTimeLeft($SESS_ID)
  {
    $ci = &get;_instance();
    $SessTimeLeft    = 0;
    $SessExpTime     = $ci->config->config["sess_expiration"];
    $CurrTime        = time();
    
    $SQL = 'SELECT "last_activity"
        FROM '.$ci->db->dbprefix.' "CI_SESSIONS" WHERE "session_id" = '." '".$SESS_ID."' ";
    //print "$SQL";
    $query = $ci->db->query($SQL);
    $arrLastActivity = $query->result_array();
    //print "LastActivity: ".$arrLastActivity[0]["last_activity"]."\r\n";
    //print "CurrentTime: ".$CurrTime."\r\n";
    //print "ExpTime: ".$SessExpTime."\r\n";
    $SessTimeLeft = ($SessExpTime - ($CurrTime - $arrLastActivity[0]["last_activity"]));
    print $SessTimeLeft;
  }
    
  function keepAlive()
  {
    $this->load->library('session');
    if(isset($this->session->userdata["USER_ID"])) print 'ALIVE';
    else                           print  '';
  }

And that's it, hope this helps

Thanks,
Bhargav Khatana
#4

[eluser]renownedmedia[/eluser]
Nice nice. I haven't used any MY_* features of CI yet, I'm sure I will have to soon!




Theme © iAndrew 2016 - Forum software by © MyBB