EllisLab text mark
Advanced Search
     
Losing my session with AJAX calls
Posted: 07 November 2011 01:18 PM
Joined: 2011-10-22
4 posts

Hi everyone,

First of, I know this is a known problem and I’ve just spent 3 hours going through the solutions people already posted on this forum about this specific problem. Though nothing worked and I’m running out of ideas.

I created my own session class like suggested here: http://ellislab.com/forums/viewthread/138823/, changed the config to increase the sess_time_to_update, etc.

Nothing worked and I still lose my session.
Here’s the relevant controller code from my app, nothing fancy really:

public function interest($categoryName "")
  
{
    error_log
(print_r($this->input->request_headers(), TRUE));

    if (!empty(
$categoryName))
    
{
      $this
->load->library('session');

      if (!isset(
$this->session->userdata['userID']))
        
show_error("blablabla"403);
      
[...

As you can see I print the headers and I do receive a cookie with the session_id, ci_session, user_agent,...

 
Posted: 07 November 2011 04:26 PM   [ # 1 ]   [ Rating: 0 ]
Avatar
Joined: 2009-06-19
6707 posts
/**
 * ------------------------------------------------------------------------
 * 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();

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

}

// ------------------------------------------------------------------------
/* End of file MY_Session.php */
/* Location: ./application/libraries/MY_Session.php */ 

 

 Signature 

Certified State of CT Computer Programming Teacher.
Custom Designed Icons, eBook Covers Software Boxes. CD, DVD Etc. New iPhone® Tab Bar Icons and iPhone® Applications Icons.

Skype: insitfx

STOP! Before posting your questions, remember the WWW Golden rule:
What did you try? What did you get? What did you expect to get?

Input -> Controller | Processing -> Model | Output -> View

 
Posted: 08 November 2011 03:44 AM   [ # 2 ]   [ Rating: 0 ]
Joined: 2011-10-22
4 posts

Thank you for you answer but I’ve seen your posts in some similar forum topics, I’ve tried it but it did not change anything… I will try it again though, just in case.

 
Posted: 08 November 2011 08:44 AM   [ # 3 ]   [ Rating: 0 ]
Avatar
Joined: 2009-06-19
6707 posts

Did you set the session tables user_agent field to varchar(120) ?

 Signature 

Certified State of CT Computer Programming Teacher.
Custom Designed Icons, eBook Covers Software Boxes. CD, DVD Etc. New iPhone® Tab Bar Icons and iPhone® Applications Icons.

Skype: insitfx

STOP! Before posting your questions, remember the WWW Golden rule:
What did you try? What did you get? What did you expect to get?

Input -> Controller | Processing -> Model | Output -> View

 
Posted: 08 November 2011 12:18 PM   [ # 4 ]   [ Rating: 0 ]
Joined: 2011-10-22
4 posts
InsiteFX - 08 November 2011 08:44 AM

Did you set the session tables user_agent field to varchar(120) ?

I do not save the sessions into my database.

So, it turns out I was already using your class. The strange thing is that everything works fine inside the class, I do have all the session items after the sess_update() but I still lose the session in my controller.

So basically:

/* MY_Session class */
public function sess_update()
    
{
        $CI 
get_instance();

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

        error_log
(print_r($this->userdatatrue)); // everything is fine here

    
/* Controller */
$this->load->library('session');
error_log(print_r($this->session->userdatatrue)); // nothing 
 
Posted: 10 November 2011 03:47 PM   [ # 5 ]   [ Rating: 0 ]
Joined: 2011-10-22
4 posts

Well I don’t know what should be my next move here. I tried several things without success and CI seems tricky to debug using tools like Xdebug. So should I just look for another PHP framework?

 
Posted: 19 November 2011 11:45 PM   [ # 6 ]   [ Rating: 0 ]
Joined: 2011-07-12
5 posts

Ignore please.  The issue can coming from another source.  Nothing to do with this code.  It works perfectly as expected smile