du0173 2010-05-11 10:15
浏览 66
已采纳

如何警告或警告用户会话将很快在php codeigniter中过期

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

The ideal solution will be count down notification that will have an option to renew the session.

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

  • 写回答

4条回答 默认 最新

  • doubi3996 2010-07-09 14:55
    关注

    Added this script in view: `

    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 Users 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  '';
    } 
    

    `

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号
  • ¥50 安装pyaudiokits失败
  • ¥15 计组这些题应该咋做呀
  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?
  • ¥15 让node服务器有自动加载文件的功能
  • ¥15 jmeter脚本回放有的是对的有的是错的
  • ¥15 r语言蛋白组学相关问题
  • ¥15 Python时间序列如何拟合疏系数模型