How to prevent auto logout in codeigniter?

2019-04-19 06:47发布

问题:

i am using codeigniter with ion_auth configured ,and MySQL as back-end, my app run smoothly but sometime/not randomly when i call add/update functions it automatically log me out. i am working on it for last 1 months but found no solution so far ? i also change setting in ion_auth config file.

$config['user_expire']  = 0;

any idea ,solution to this problem?
please comment ,so that i can provide more data if needed.

Note: i have also check this but no luck.

回答1:

You are probably performing ajax requests, this is a common issue...

I would suggest you to use session database and make ajax calls is to not update the session...

Make this on you session class

class MY_Session extends CI_Session {

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

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


回答2:

Create a session library with your own MY_Session.php library that overwrote the sess_update method with one that only executed the update method when not an AJAX request:

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();
    }

}

Location of file:

/application/libraries/MY_Session.php */

You can either auto-load this library from config/autoload.php:

$autoload['libraries'] = array( 'MY_Session');

Or, you can load it later:

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

What this sess_update(); does?

In your system/libraries/Session.php there is a function sess_update() that automatically update your last activity.This function update the session every five minutes by default.

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);
    }


回答3:

Replace line 346 in system/libraries/Session.php (function sess_update())

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

With:

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

hope this works for you .



回答4:

this is best answer.

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)
                       {
          }
                     });