Part of the EllisLab Network
   
2 of 2
2
AJAX and CI Session (v1.7) w/DB
Posted: 13 May 2009 05:33 AM   [ Ignore ]   [ # 16 ]  
Summer Student
Total Posts:  3
Joined  05-13-2009

Hi, I’m having the same problem. I’m starting to add bits of AJAX functionality into a couple of websites.

We have a ‘Change Details’ page and I have developed a little ‘username checker’ in mootools. It basically does an AJAX request, checking if the username already exists in the system. Should be simple enough I thought.

The problem was that the AJAX requests seemed to log the user out. I’m now assuming I have the same problem with the session id changing on the AJAX request and the parent page is then out of date.

My initial fix was to add this to the constants.php config file

define('IS_AJAX', isset($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest'); 

Then this in the config.php file

if(IS_AJAX){
    $config[
'sess_time_to_update']     72000;
}else{
    $config[
'sess_time_to_update']     300;

I initially thought it was working but it seems to be a false hope! I’ve tested it with debug code and the config is being set ($config[‘sess_time_to_update’] = 72000, expiration is 7200) but that doesn’t fix it.

Is this a different problem (tearing my hair out over this one!)

Profile
 
 
Posted: 13 May 2009 09:26 AM   [ Ignore ]   [ # 17 ]  
Research Assistant
Avatar
RankRankRank
Total Posts:  549
Joined  07-28-2008

edit: sorry, that was for another fix :( lol… 

I have commented out the session id regernation line in the session class.

With your above, are you making sure to set the HTTP_X_REQUESTED_WITH via the ajax call? This needs to be manually set for it to be recognized.

 Signature 

~ 4 All the Right Reasons ~

Profile
 
 
Posted: 13 May 2009 10:38 AM   [ Ignore ]   [ # 18 ]  
Summer Student
Total Posts:  3
Joined  05-13-2009

Thanks for the reply.

I checked that the HTTP_X_REQUESTED_WITH was being recognised and indeed it was.

I have now sorted the problem - it was a mixture of a couple of things. One being the above problem, I solved this by recreating the session class, and altering the sess_update() method…

function sess_update()
    
{

        
// Check IS_AJAX constant!
          
if (IS_AJAX){
            
return; 
        
}
        
..... 


The other problem with actually elsewhere in my code - I was trying to manually refresh the session on every refresh. I’ve fixed that as well and it now all seems to work!

Profile
 
 
Posted: 14 May 2009 05:34 AM   [ Ignore ]   [ # 19 ]  
Sr. Research Associate
Avatar
RankRankRankRankRank
Total Posts:  4102
Joined  11-04-2008

It would be simpler to just use the original method, by putting this in your MY_SESSION library:

/**
 * Update an existing session
 *
 * @access    public
 * @return    void
*/
function sess_update()
{
   
// skip the session update if this is an AJAX call!
   
if ( !IS_AJAX )
   
{
       parent
::sess_update();
   
}

This way you don’t have to copy the entire function, and run into issues when a new CI version comes with an updated session library.

 Signature 

WanWizard.eu | Modular CI, an HMVC solution | DataMapper ORM

Profile
 
 
Posted: 14 May 2009 05:43 AM   [ Ignore ]   [ # 20 ]  
Summer Student
Total Posts:  3
Joined  05-13-2009

that’s even better - thanks!

Profile
 
 
Posted: 27 January 2011 10:11 AM   [ Ignore ]   [ # 21 ]  
Summer Student
Avatar
Total Posts:  16
Joined  09-29-2010

well, i haven’t the second page, so i reconstruct my method, before i change my class, i sent for the model severy ajax request wich haves diferent ids, now i send one ajax request with an array, and in the model i make an loop wich retrieves each id.

Well, i spent 2 hours or more fixing my methods, but it could be an way…

Profile
 
 
Posted: 25 October 2011 03:04 PM   [ Ignore ]   [ # 22 ]  
Summer Student
Total Posts:  3
Joined  10-25-2011

This is an old thread, but for folks who come across it in the archives, I’ve posted a workaround for CI 2.0.2 here:

https://github.com/EllisLab/CodeIgniter/issues/154

Was based on inspiration from this thread.

Profile
 
 
Posted: 25 October 2011 06:00 PM   [ Ignore ]   [ # 23 ]  
Sr. Research Associate
Avatar
RankRankRankRankRank
Total Posts:  2520
Joined  02-19-2009

@dordal- FYI CI 2+ has $this->input->is_ajax_request() method for detecting ajax.

 Signature 
Profile
 
 
Posted: 26 October 2011 06:31 PM   [ Ignore ]   [ # 24 ]  
Summer Student
Total Posts:  3
Joined  10-25-2011

Thank you. I updated the GitHub post.

Profile
 
 
Posted: 07 February 2012 05:50 PM   [ Ignore ]   [ # 25 ]  
Summer Student
Avatar
Total Posts:  16
Joined  09-29-2010

For anyone who get the same problem, and maybe can’t just don’t update ajax calls, just follow this link and everything will work, you will lose a litlle of security, but for me it will be ok.

The security problem it’s because your session_id will never get updated… better than don’t update nothing… Was my problem because i’m using Extjs framework at frontend, and in this system you don’t have any refresh on the browser, everything are made with ajax calls…

Luck 4 everyone, and God bless you!

Profile
 
 
Posted: 07 February 2012 07:44 PM   [ Ignore ]   [ # 26 ]  
Summer Student
Avatar
Total Posts:  16
Joined  09-29-2010

I did fill changes on the else state:

else {
    $new_sessid 
$old_sessid;
    
$this->userdata['session_id'$old_sessid;
Profile
 
 
   
2 of 2
2