Welcome Guest, Not a member yet? Register   Sign In
Alert User Before Session Times Out
#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


Messages In This Thread
Alert User Before Session Times Out - by El Forum - 08-18-2009, 03:57 PM
Alert User Before Session Times Out - by El Forum - 08-18-2009, 05:21 PM
Alert User Before Session Times Out - by El Forum - 08-20-2009, 11:37 AM
Alert User Before Session Times Out - by El Forum - 08-20-2009, 01:35 PM



Theme © iAndrew 2016 - Forum software by © MyBB