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

Abd*_*nan 14 php mysql codeigniter ion-auth

我使用codeigniterion_auth配置,以及MySQL作为后端,我的应用程序流畅运行,但有时/不是随机当我打电话add/update的功能,它会自动登录我出去.我正在研究它最近1个月但到目前为止找不到解决方案?我也改变了ion_auth配置文件中的设置.

$config['user_expire']  = 0;
Run Code Online (Sandbox Code Playgroud)

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

注意:我也检查过这个,但没有运气.

小智 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();
    }
}
}
Run Code Online (Sandbox Code Playgroud)


Sat*_*aty 7

使用您自己的MY_Session.php库创建一个会话库,该库使用sess_update仅在不是AJAX请求时执行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();
    }

}
Run Code Online (Sandbox Code Playgroud)

文件位置:

/application/libraries/MY_Session.php*/

您可以从中自动加载此库 config/autoload.php:

$autoload['libraries'] = array( 'MY_Session');
Run Code Online (Sandbox Code Playgroud)

或者,您可以稍后加载它:

$this->load->library('MY_Session');
Run Code Online (Sandbox Code Playgroud)

这是什么sess_update(); 呢?

在你system/libraries/Session.php有一个function sess_update()自动更新您的最后activity.This功能更新会话,默认为每五分钟.

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);
    }
Run Code Online (Sandbox Code Playgroud)