如何防止在codeigniter中自动注销?

时间:2015-04-22 13:30:31

标签: php mysql codeigniter ion-auth

我正在使用codeigniter  配置ion_auth,MySQL作为后端, 我的应用程序运行顺利,但有时/不随机,当我调用add/update函数时,它会自动将我注销。 我正在研究它最近1个月但到目前为止找不到解决方案? 我还更改了ion_auth配置文件中的设置。

$config['user_expire']  = 0;

任何想法,解决这个问题?
请评论,以便我可以在需要时提供更多数据。

注意:我还检查this但没有运气。

4 个答案:

答案 0 :(得分:8)

您可能正在执行ajax请求,这是一个常见问题......

我建议您使用会话数据库并使ajax调用不更新会话...

在会话课上进行此操作

class MY_Session extends CI_Session {

public function sess_update()
{
    $CI =& get_instance();

    if ( ! $CI->input->is_ajax_request())
    {
        parent::sess_update();
    }
}
}

答案 1 :(得分:6)

使用您自己的MY_Session.php库创建一个会话库,该库使用仅在不是AJAX请求时执行更新方法的sess_update方法覆盖:

<强> MY_Session.php

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

require_once BASEPATH . '/libraries/Session.php';

class MY_Session extends CI_Session
{

    function __construct()
    {
        parent::__construct();

        $this->CI->session = $this;
    }

    function sess_update()
    {
        // Do NOT update an existing session on AJAX calls.
        if (!$this->CI->input->is_ajax_request())
            return parent::sess_update();
    }

}

档案位置:

  

/application/libraries/MY_Session.php * /

您可以从config/autoload.php:

自动加载此库
$autoload['libraries'] = array( 'MY_Session');

或者,您可以稍后加载:

$this->load->library('MY_Session');

这是什么sess_update();确实吗

system/libraries/Session.php中,function sess_update()会自动更新您的上一个活动。此功能默认情况下每五分钟更新一次会话。

public function sess_update()
    {
        // We only update the session every five minutes by default
        if (($this->userdata['last_activity'] + $this->sess_time_to_update) >= $this->now)
        {
            return;
        }

        // _set_cookie() will handle this for us if we aren't using database sessions
        // by pushing all userdata to the cookie.
        $cookie_data = NULL;

        /* Changing the session ID during an AJAX call causes problems,
         * so we'll only update our last_activity
         */
        if ($this->CI->input->is_ajax_request())
        {
            $this->userdata['last_activity'] = $this->now;

            // Update the session ID and last_activity field in the DB if needed
            if ($this->sess_use_database === TRUE)
            {
                // set cookie explicitly to only have our session data
                $cookie_data = array();
                foreach (array('session_id','ip_address','user_agent','last_activity') as $val)
                {
                    $cookie_data[$val] = $this->userdata[$val];
                }

                $this->CI->db->query($this->CI->db->update_string($this->sess_table_name,
                                            array('last_activity' => $this->userdata['last_activity']),
                                            array('session_id' => $this->userdata['session_id'])));
            }

            return $this->_set_cookie($cookie_data);
        }

        // Save the old session id so we know which record to
        // update in the database if we need it
        $old_sessid = $this->userdata['session_id'];
        $new_sessid = '';
        do
        {
            $new_sessid .= mt_rand(0, mt_getrandmax());
        }
        while (strlen($new_sessid) < 32);

        // To make the session ID even more secure we'll combine it with the user's IP
        $new_sessid .= $this->CI->input->ip_address();

        // Turn it into a hash and update the session data array
        $this->userdata['session_id'] = $new_sessid = md5(uniqid($new_sessid, TRUE));
        $this->userdata['last_activity'] = $this->now;

        // Update the session ID and last_activity field in the DB if needed
        if ($this->sess_use_database === TRUE)
        {
            // set cookie explicitly to only have our session data
            $cookie_data = array();
            foreach (array('session_id','ip_address','user_agent','last_activity') as $val)
            {
                $cookie_data[$val] = $this->userdata[$val];
            }

            $this->CI->db->query($this->CI->db->update_string($this->sess_table_name, array('last_activity' => $this->now, 'session_id' => $new_sessid), array('session_id' => $old_sessid)));
        }

        // Write the cookie
        $this->_set_cookie($cookie_data);
    }

答案 2 :(得分:1)

替换system / libraries / Session.php中的第346行(函数sess_update())

if (($this->userdata['last_activity'] + $this->sess_time_to_update) >= $this->now)

使用:

if (($this->userdata['last_activity'] + $this->sess_time_to_update) >= $this->now || $this->CI->input->is_ajax_request())

希望这适合你。

答案 3 :(得分:-1)

这是最好的答案。

codeignter controller     
    function fetchSendToProduction($sku,$old_fab_id)
            {
                 if($this->input->is_ajax_request() > 0)
                 {     
                       $result = $this->ifmodel->fetchSendToProduction($sku,$old_fab_id);
                       echo json_encode($result);
                 }
            }
       codeignter view ajax call    
         $.ajax({
                        type: "POST",
                        url: "<?php echo base_url(); ?>index.php/inputFactor/fetchSendToProduction/" + sku+'/'+old_fab_id ,
                        cache: false,
                        processData: false,
                        success: function(data)
                       {
          }
                     });