Welcome Guest, Not a member yet? Register   Sign In
Losing session user_data randomly
#1

[eluser]Unknown[/eluser]
I've searched through these forums pretty extensively but haven't managed to find something that's exactly the same as my problem or that helps me fix it.

I'm running into an issue where I'm losing the information stored in the user_data column of my db for the current session.

The flow is this;

User attempts to log in ->
Controller updates the session via Ajax call and responds to the calling page ->
If the log in was successful then javascript redirects to the 'logged in' page.

The problem I'm having is about 1 in 5 times this flow happens, the user ends up at the logged in page but with no data set in the user_data for the session - which essentially makes that user logged out even after successfully logging in.

This is not an issue due to underscores in the session cookie name (there aren't), it's not limited to a single browser (seen in FF, IE and Safari independently).

I implemented logging in the controller that's called for signing in and the page that's redirected to. The logging happens at the end of the sign in controller and the beginning of the redirected page's controller.

The logging shows that session_id, ip_address, user_agent, last_activity all stay consistent. The user_data that is logged at the end of the sign in controller is lost after the redirect (the data is empty).

This problem does not happen every time. I can log in 10 times in a row succesfully then randomly twice in a row it will fail. Like I said... maybe 1 in 5 is the average failure rate.

Any ideas? I'm sure it's something glaringly obvious but I really don't know where the session data could be being reset since I don't know what's happening between the two steps server side (ending one controller, then being redirected to a new one through javascript on the front end).

Thanks for any input!
#2

[eluser]InsiteFX[/eluser]
application/config/constants.php
Code:
// Define Ajax Request
define('IS_AJAX', isset($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest');

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

/**
* ------------------------------------------------------------------------
* CI Session Class Extension.
* ------------------------------------------------------------------------
*
*
*/

class MY_Session extends CI_Session {
   /*
    * Do not update an existing session on ajax calls
    *
    * @access    public
    * @return    void
    */
    public function sess_update()
    {
        if ( ! IS_AJAX)
        {
            parent::sess_update();
        }
    }

    function sess_destroy()
    {
        parent::sess_destroy();

        $this->userdata = array();
    }

}

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

InsiteFX
#3

[eluser]Unknown[/eluser]
As much as I wish this was the case, adding or removing the functionality you're suggesting makes no difference whatsoever to this issue. I should have mentioned I'd already tried implementing that after browsing these forums.

Thanks for the input though Smile
#4

[eluser]WanWizard[/eluser]
Database sessions? Cookie sessions? Alternative session library?
#5

[eluser]ray023[/eluser]
[quote author="suicidalandroid" date="1286527399"]
...Any ideas?[/quote]

I have an idea because I've been chasing a similar problem the past couple of days.

Are you setting session values to the use the database:
Code:
$config['sess_use_database']    = TRUE;

If so, the value in your user_data field might be going over the field's limit; which will cause CI to destroy the session var (See Saving Session Data to a Database)

UPDATE: Now that I've taken a closer look at my database, I noticed that I had user_data set to varchar(255) instead of TEXT Tongue

You probably have paid a bit more attention to detail than I have for your issue...but I will leave this post in tact on the chance it proves helpful.
#6

[eluser]SPeed_FANat1c[/eluser]
I have very similar problem. But it only happens when administrator is working. When admin logins then sessions userdata becomes like this:

a:2:{s:8:"username";s:5:"admin";s:12:"is_logged_in";s:1:"1";}

when admin start working, then userdata becomes bigges, it holds filter, offset:

a:4:{s:8:"username";s:5:"admin";s:12:"is_logged_in";s:1:"1";s:19:"objektu_sar_filtras";s:10:"index_ajax";s:6:"offset";s:1:"0";}

BTW session get updated this way every time ajax is called:

Code:
$rodymas = array('objektu_sar_filtras' => 'index_ajax',
            'offset' => $offset);
            
$this->session->set_userdata($rodymas);

It works ok, but then at some time it randomly becomes only

a:2:{s:19:"objektu_sar_filtras";s:10:"index_ajax";s:6:"offset";s:2:"12";}

There isn't username and is_logged_in variable anymore.

I could think maybe it becomes so when I update when call ajax by not updating username and is_logged_in variable. But then it should instatnly log out after ajax call.



Edit:

Found that this actually is not random. Wit the user that has not admin rights it also happens, and yet I found that user becomes not logged in after "back" button in browser is pressed. Not always, but in some controllers. But still in the ci_sessions table user is logged in, but it gets userdata that he ins't logged int. I made a test controller with function to see

Code:
function index()
    {
      
        
    
        echo $this->session->userdata('is_logged_in').br();
        echo $this->session->userdata('objektu_sar_filtras').br();
        
        
    }

and exactly - he isn't logged in.
So it means that session class thinks that user is not logged, although he is. Why could it be?
#7

[eluser]WanWizard[/eluser]
This is a common issue when using sessions and ajax calls. Search for IS_AJAX.
#8

[eluser]SPeed_FANat1c[/eluser]
It looks like it works now Smile at least after few tests. Thank you Smile So it means something like id in table is updated, but there is still session cookie and it does not match with id from table, and session cookie is not updated because not the whole page is loaded. Or something like that Smile

BTW there is small error in InsiteFX post - it should be

Code:
if ( ! IS_AJAX)

not
Code:
if ( ! IS_AJAX())
#9

[eluser]InsiteFX[/eluser]
DEFINE in application/config/constants.php
Code:
// Define Ajax Request
define('IS_AJAX', isset($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest');

EDITED: 11-04-2010

InsiteFX




Theme © iAndrew 2016 - Forum software by © MyBB