• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
AJAX calls kill sesion

#1
Hi,
i have 2 sites with codeigniter , and in one of these, have a lot ajax call (like a ajax chat), in this case i have to use a "patch" with :
PHP Code:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

require_once 
SYSDIR '/libraries/Driver.php';
require_once 
SYSDIR '/libraries/Session/Session.php';
require_once 
SYSDIR '/libraries/Session/drivers/Session_cookie.php';

class 
MY_Session_cookie extends CI_Session_cookie
{

    public function 
__construct()
    {
        
parent::__construct();
        
log_message('info''MY_Session_cookie loaded');
    }

    protected function 
_sess_update($force false)
    {

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

}

/* End of file MY_Session_cookie.php */
/* Location: ./system/libraries/Session/drivers/MY_Session_cookie.php */ 

This solve constantly logouts for a while, but some times take logouts again, and speciallly when a ajax call was perform when i change current tab, and click in another with no site content(in that case logout is inmediatly when performe a ajax).

i Notice that cookie is correctly send it to validate, but the answer come with another cookie (becouse session expires i supost).. any idea?
Reply

#2
This was one of the bugs that never got fixed in the Codeigniter 2.x branch. I'm pretty confident it was fixed in the 3.x branch which is still under development. You can download the latest version on github though.
Reply

#3
(12-15-2014, 01:58 PM)albertleao Wrote: This was one of the bugs that never got fixed in the Codeigniter 2.x branch. I'm pretty confident it was fixed in the 3.x branch which is still under development. You can download the latest version on github though.

i was test it on 2.2 and now i'm using the latest version 3.x, hopping fix that bug.
Reply

#4
This has always worked for me give it a try.

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

/**
* ------------------------------------------------------------------------
* Created by PhpStorm 8.0.3.
* Date : 5/6/2012
* Time : 11:19:06 PM
* ------------------------------------------------------------------------
*
* Class MY_Session
*
* @package        Package        CodeIgniter
* @subpackage    Subpackage    session
* @category    category    sessions
* @author        Raymond L King Sr.
* @link        http://example.com
* ------------------------------------------------------------------------
* To change this template use File | Settings | File Templates.
* ------------------------------------------------------------------------
*/

/**
* ------------------------------------------------------------------------
* CI Session Class Extension for AJAX calls.
* ------------------------------------------------------------------------
*
* ====- Save as application/libraries/MY_Session.php -====
*/

class MY_Session extends CI_Session {

    // --------------------------------------------------------------------

    /**
     * sess_update()
     *
     * Do not update an existing session on ajax or xajax calls
     *
     * @access    public
     * @return    void
     */
    public function sess_update()
    {
        $_ci =& get_instance();

        $ajax = $_ci->input->is_ajax_request();

        if ($ajax === FALSE)
        {
            parent::sess_update();
        }
    }

    // --------------------------------------------------------------------

    /**
     * sess_destroy()
     *
     * Clear's out the user_data array on sess::destroy.
     *
     * @access    public
     * @return    void
     */
    public function sess_destroy()
    {
        $this->userdata = array();

        parent::sess_destroy();
    }

}    // End of Class.

/**
* ------------------------------------------------------------------------
* Filename: MY_Session.php
* Location: ./application/libraries/MY_Session.php
* ------------------------------------------------------------------------
*/
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply

#5
(12-15-2014, 02:47 PM)InsiteFX Wrote: This has always worked for me give it a try.


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

/**
* ------------------------------------------------------------------------
* Created by PhpStorm 8.0.3.
* Date : 5/6/2012
* Time : 11:19:06 PM
* ------------------------------------------------------------------------
*
* Class MY_Session
*
* @package Package CodeIgniter
* @subpackage Subpackage session
* @category category sessions
* @author Raymond L King Sr.
* @link http://example.com
* ------------------------------------------------------------------------
* To change this template use File | Settings | File Templates.
* ------------------------------------------------------------------------
*/

/**
* ------------------------------------------------------------------------
* CI Session Class Extension for AJAX calls.
* ------------------------------------------------------------------------
*
* ====- Save as application/libraries/MY_Session.php -====
*/

class MY_Session extends CI_Session {

// --------------------------------------------------------------------

/**
* sess_update()
*
    * Do not update an existing session on ajax or xajax calls
    *
    * @access    public
    * @return    void
    */
   public function sess_update()
{
$_ci =& get_instance();

$ajax = $_ci->input->is_ajax_request();

if ($ajax === FALSE)
{
parent::sess_update();
}
   }

// --------------------------------------------------------------------

/**
* sess_destroy()
*
    * Clear's out the user_data array on sess::destroy.
    *
    * @access    public
    * @return    void
    */
public function sess_destroy()
{
$this->userdata = array();

parent::sess_destroy();
}

} // End of Class.

/**
* ------------------------------------------------------------------------
* Filename: MY_Session.php
* Location: ./application/libraries/MY_Session.php
* ------------------------------------------------------------------------
*/
Must be loaded or autoloaded?, and well now i'm using CI3 do you know if work on it?,(by the way i'll test it on CI3 and give a comment)
Reply

#6
Not sure if it will work on CI 3.0, I do not use it until it is released.

But it does work for me under CI 2.2.0
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply

#7
(12-16-2014, 05:15 AM)InsiteFX Wrote: Not sure if it will work on CI 3.0, I do not use it until it is released.

But it does work for me under CI 2.2.0

Hi,
Must be loaded or autoloaded?
Reply

#8
well making this file seems to work on CI3, but not fix logout on tabs change (firefox BTW), and this file don't need to be autoloaded, placing this on correct directory, autoload the file.

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

require_once 
SYSDIR '/libraries/Driver.php';
require_once 
SYSDIR '/libraries/Session/Session.php';
require_once 
SYSDIR '/libraries/Session/drivers/Session_cookie.php';

class 
MY_Session_cookie extends CI_Session_cookie
{

    public function 
__construct()
    {
        
parent::__construct();
        
log_message('info''MY_Session_cookie loaded');
    }

    protected function 
_sess_update($force false)
    {

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

        /**
        * sess_destroy()
        *
            * Clear's out the user_data array on sess::destroy.
            *
            * @access    public
            * @return    void
            */
        
public function sess_destroy()
        {
        
$this->userdata = array();

        
parent::sess_destroy();
        }

}

/* End of file MY_Session_cookie.php */
/* Location: ./system/libraries/Session/drivers/MY_Session_cookie.php */ 
Reply

#9
https://github.com/bcit-ci/CodeIgniter/i...t-67228008

Please don't concurrently post here and on GitHub ... If you're looking to solve a problem that you're having, post here and only post on GitHub if you're sure that it's a bug that hasn't already been fixed.
Reply


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2019 MyBB Group.