How to warn or warn user that session will expire soon in php codeigniter

Basically I'm looking for a solution where the user is notified five minutes before the session expires.

The ideal solution would be to count the notification that will be able to resume the session.

If the countdown timer expires without the user refreshing the page, I need to log them out.

+2


a source to share


4 answers


Added this script to sight: `

if(isSessionAlive >0)
{
    var timer = {
        time: 0,
        now: function(){ return (new Date()).getTime(); },
        start: function(){ this.time = this.now(); },
        since: function(){ return this.now()-this.time; }
    }
    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*1000)));
    jsBaseurl  =  "<?=($this->config->config["base_url"])?>";

}
function resetTimer(resetTime)
{
    //alert('RESET Time'+resetTime);
    window.clearTimeout(timerId);
    timerId = window.setTimeout("pingCI()", resetTime);
    return;
}
function pingCI()
{
    if(isSessionAlive > 0)
    {
            $.ajax({
                type: "POST",
                url: "<?= site_url('users/getSessionTimeLeft') ?>/",
                data: "sessid=<?=$this->session->userdata("session_id")?>",
                success: function(transport) 
                {
                    response = transport;

                    if(response=='')
                    {
                        parent.location.assign(jsBaseurl+'users/logout');
                    }
                    else if((response<=(alertTime*1000)) ||  (response-1000<=(alertTime*1000)))
                    {
                        alertSessionTimeOut(response);
                    }
                    else
                    {
                        resetTime = eval((response - alertTime)*1000);
                        resetTimer(resetTime);
                    }
                } 
            });
    }
}
function alertSessionTimeOut(alertTimeExp)
{
    if(isSessionAlive>0)
    {
        var response='';
        var timerIdEnd;

        timerAlert = window.setTimeout("forceLogout()",alertTimeExp*1000);
        timer.start(); // start counting my friend...


        fConfirm = confirm('Your Session is about to time out. Please click OK to continue the session');
        if(timer.since() >= (alertTime*1000))
        {
            parent.location.assign(jsBaseurl+'users/logout');
        }
        if(fConfirm ==true)
        {
                $.ajax({
                    type: "POST",
                    url: "<?= site_url('users/keepAlive') ?>/",
                    data: "sessid=<?=$this->session->userdata("session_id")?>",
                    success: function(transport) 
                    {
                        response = transport;
                        if(response=='')
                        {
                            parent.location.assign(jsBaseurl+'users/logout');
                        }
                        window.clearTimeout(timerAlert);
                        resetTimer(sess_expiration-((alertTime)*1000));
                    }
                });
        }
        else
        {
            //parent.location.assign(jsBaseurl+'users/logout');
            window.clearTimeout(timerAlert);
            window.clearTimeout(timerId);

        }
    }
}

function forceLogout()
{
    parent.location.assign(jsBaseurl+'users/logout');
} 

      

And in the user controller:



   function getSessionTimeLeft()
   {
    $ci = & get_instance();
    $SessTimeLeft    = 0;
    $SessExpTime     = $ci->config->config["sess_expiration"];
    $CurrTime        = time();
    $lastActivity = $this->session->userdata['last_activity'];
    $SessTimeLeft = ($SessExpTime - ($CurrTime - $lastActivity))*1000;
    print $SessTimeLeft;
}

function keepAlive()
{
    $this->load->library('session');
    $this->session->set_userdata(array('last_activity'=>time()));
    if(isset($this->session->userdata["user_id"])) print 'ALIVE';
    else print  '';
} 

      

`

+1


a source


Since the session will be updated as soon as you go back to the server side and the script will call session_start (), you really need to do this in Javascript. However, if the user has two browser windows open with a split session, and one is inactive, and the user is still generating traffic with the other, then the javascript in the unoccupied window incorrectly reports that the session is about to expire. Therefore, you will need to implement your own ajax wrapper to determine the age of the session without calling session_start ().

Sort of:



 $session_id=$_REQUEST[session_name()];
 // if you use the default handler:
 $session_last_access=filemtime(session_save_path() . '/' . $session_id);
 $time_left=time() + session_cache_expire() - $session_last_access;

      

FROM.

+2


a source


Depending on what exactly you want to achieve. When someone is using multiple tabs / windows, the window can stay open for a very long time without the session expiring. AJAX operations further complicate matters. If you want to receive accurate notifications, you will need to set up a timer and when it starts to light, check via an AJAX request (without worrying about resuming the session) if the estimate is still accurate.

+1


a source


  • One way is to store the remaining time in one javascript variable and update the variable on every page refresh

  • Create a single javascript function with one parameter that checks the value of the variable set to 1.)

Regards,
Pedro

0


a source







All Articles