Part of the EllisLab Network
   
1 of 2
1
[SOLVED] Session userdata is being lost
Posted: 29 June 2009 04:09 AM   [ Ignore ]  
Grad Student
Avatar
Rank
Total Posts:  37
Joined  06-07-2009

Hello everybody!

I’m making a small project which requires a user login. Nothing fancy, no user roles. I’ve used CodeIgniter for this before. I just store the username in the session userdata. However I ran into a problem this time.
After login $this->session->userdata(‘username’) is reported as empty every time.
I var_dump $this->session->userdata and I see it is being set in the login method. However after the redirect the username userdata is non-existent. If I check in the database the session is being stored correctly, with the same session ID and the userdata also containing the stuff I expect.

Does anyone have any clues about this? I’ve been stuck at this simple thing for two days!

I forgot to mention that the database is Oracle. I don’t know if this has anything to do with this, but I doubt. The queries run fine and no errors are being reported.

Just for the reference, here’s the login method.

public function login()

    
{
        $this
->form_validation->set_rules('username''Username''required|trim');
        
$this->form_validation->set_rules('password''Password''required|trim');
        
$this->form_validation->set_error_delimiters('<div class="error">''</div>');
        if (
$this->form_validation->run() == false)
        
{
            $data[
'html_title''Login';
            
$this->load->view('auth/login',$data);
        
else {
            $this
->db->where('username',$this->input->post('username'));
            
$this->db->where('password',md5($this->input->post('password')));
            
$query $this->db->get($this->tables['users']);
            if (
$query->num_rows() == 1{
                $result 
$query->row();
                
$data = array(
                    
'username' => $this->input->post('username'),
                    
'logged_in' => true
                
);
                
$this->session->set_userdata($data);


                
redirect('dashboard');
            
else {
                $this
->session->set_flashdata('message','<div class="error">Error logging in. Check your username and password.</div>');
                
redirect('auth/login');
            
}
        }

    } 

Thanks in advance!

 Signature 

lose > hesitate

Profile
 
 
Posted: 29 June 2009 07:39 AM   [ Ignore ]   [ # 1 ]  
Sr. Research Associate
Avatar
RankRankRankRankRank
Total Posts:  3896
Joined  04-25-2008

Does the session ID change on each request? Is it possible that you are somehow clearing the data on each request, perhaps in your login/initialisation routines?

 Signature 

Remember the 8 Ps: Perfect Planning and Prior Preparation Prevents Piss-Poor Performance.


Not sure where to start with your project? Need some inspiration? Check out my CodeIgniter Resources thread

Profile
 
 
Posted: 29 June 2009 07:56 AM   [ Ignore ]   [ # 2 ]  
Grad Student
Avatar
Rank
Total Posts:  37
Joined  06-07-2009

The Session ID stays the same, the database doesn’t contain multiple session id’s neither. It’s just my session ID in there since it’s a test server and I’m the only one using it. The session class is in the autoload config. Apart from that I’m not using any initialisations of any kind. The only place I’m destroying the session is in the logout method which is called only on request. But just to be safe I’ve commented the logout method and the result is the same.

 Signature 

lose > hesitate

Profile
 
 
Posted: 29 June 2009 08:01 AM   [ Ignore ]   [ # 3 ]  
Sr. Research Associate
Avatar
RankRankRankRankRank
Total Posts:  3896
Joined  04-25-2008

And you’re not calling session_start() or $this->session->sess_create() anywhere? Do all of your controllers work as you’d expect?

 Signature 

Remember the 8 Ps: Perfect Planning and Prior Preparation Prevents Piss-Poor Performance.


Not sure where to start with your project? Need some inspiration? Check out my CodeIgniter Resources thread

Profile
 
 
Posted: 29 June 2009 08:12 AM   [ Ignore ]   [ # 4 ]  
Grad Student
Avatar
Rank
Total Posts:  37
Joined  06-07-2009

No, I’m not calling session_start or sess_create() anywhere. I was under the impression this can be skipped. Isn’t the session created automatically? I can’t say if the other controllers work as expected cause they all require login. And I started from the login system and haven’t written the other controllers yet smile

 Signature 

lose > hesitate

Profile
 
 
Posted: 29 June 2009 08:23 AM   [ Ignore ]   [ # 5 ]  
Sr. Research Associate
Avatar
RankRankRankRankRank
Total Posts:  3896
Joined  04-25-2008

Yes, the session library automatically creates/reads an existing session, which is why I asked. If you called on sess_create() manually, it would kill the old session, which would explain your problem. Since you’re not, I’m not quite sure where the problem lies.

What I was referring to by the other controllers working as expected, was that you can pass in several extra segments, and CodeIgniter will process them correctly. Sometimes CodeIgniter fails to parse the URI correctly as the uri_protocol is not set correctly. Sorry for not making myself clearer. I’d suggest you create a test controller, and try it out:

<?php

class Test extends Controller
{
    
function func($arg1=""$arg2="")
    
{
        var_dump
($arg1);
        echo 
"<br />";
        
var_dump($arg2);
    
}

Obviously, you’d need to call on it something like this:

http://yoursite.tld/index.php/test/arg1/arg2

This should output something like:

string(4"arg1"
string(4"arg2" 
 Signature 

Remember the 8 Ps: Perfect Planning and Prior Preparation Prevents Piss-Poor Performance.


Not sure where to start with your project? Need some inspiration? Check out my CodeIgniter Resources thread

Profile
 
 
Posted: 29 June 2009 08:31 AM   [ Ignore ]   [ # 6 ]  
Grad Student
Avatar
Rank
Total Posts:  37
Joined  06-07-2009

Strangely, if I call upon the controller without passing the arguments, it dumps the empty strings. But when I put one or two of the arguments in the url it says 404 Not Found!
Is this what you were expecting?

 Signature 

lose > hesitate

Profile
 
 
Posted: 29 June 2009 08:34 AM   [ Ignore ]   [ # 7 ]  
Grad Student
Avatar
Rank
Total Posts:  37
Joined  06-07-2009

I’m guessing .htaccess issues?  Am I on the right path?
I should’ve stuck with the simple .htaccess from the documentation, like I usually do. But this time I decided to try something more “fancy” smile

 Signature 

lose > hesitate

Profile
 
 
Posted: 29 June 2009 08:48 AM   [ Ignore ]   [ # 8 ]  
Sr. Research Associate
Avatar
RankRankRankRankRank
Total Posts:  3896
Joined  04-25-2008

It’s not necessarily an issue with htaccess, but rather how CodeIgniter parses the URI. I’m not sure that this should not affect cookies. In the mean time, I’d suggest you try the different URI protocols in config.php, and see if that makes any different.

I recommend you get rid of your htaccess file just for now (just rename it), and concentrate on getting the test controller to work properly (you’ll need to call everything like this: http://yoursite.tld/index.php/test/arg1/arg2

Once you have this working, then it’s time to start messing about with your htaccess file. Once your controller is working without htaccess, if you’re having trouble with getting the htaccess to work, please post it and we will take a look at it.

However, please note: I don’t see the connection between this and cookies, so I’ll be surprised if this does fix your issue, but you never know. smile

 Signature 

Remember the 8 Ps: Perfect Planning and Prior Preparation Prevents Piss-Poor Performance.


Not sure where to start with your project? Need some inspiration? Check out my CodeIgniter Resources thread

Profile
 
 
Posted: 29 June 2009 08:50 AM   [ Ignore ]   [ # 9 ]  
Grad Student
Avatar
Rank
Total Posts:  37
Joined  06-07-2009

Thanks a lot for the help. I’ll give a shout when I make the uri’s work correctly.

 Signature 

lose > hesitate

Profile
 
 
Posted: 29 June 2009 09:04 AM   [ Ignore ]   [ # 10 ]  
Grad Student
Avatar
Rank
Total Posts:  37
Joined  06-07-2009

This is very strange. Neither of the uri_protocols work properly for the Test controller. Some will at least display the test controller when no arguments are passed. Others when used in the config push the system in an endless loop that the browser detects and stops the loading.

 Signature 

lose > hesitate

Profile
 
 
Posted: 29 June 2009 09:15 AM   [ Ignore ]   [ # 11 ]  
Sr. Research Associate
Avatar
RankRankRankRankRank
Total Posts:  3896
Joined  04-25-2008

Sounds like you’re htaccess hasn’t been renamed. smile

 Signature 

Remember the 8 Ps: Perfect Planning and Prior Preparation Prevents Piss-Poor Performance.


Not sure where to start with your project? Need some inspiration? Check out my CodeIgniter Resources thread

Profile
 
 
Posted: 29 June 2009 09:37 AM   [ Ignore ]   [ # 12 ]  
Grad Student
Avatar
Rank
Total Posts:  37
Joined  06-07-2009

I renamed it and doublechecked it. It was renamed in the first place, but just in case I checked again smile

 Signature 

lose > hesitate

Profile
 
 
Posted: 29 June 2009 09:41 AM   [ Ignore ]   [ # 13 ]  
Sr. Research Associate
Avatar
RankRankRankRankRank
Total Posts:  3896
Joined  04-25-2008

That’s really strange. If it’s not the htaccess file that’s causing the redirect loop, then it must be one of the redirects in your code. I can’t see how it could be any of the ones you have, since if the form is not being submitted, redirect should never get called. Strange… If you’re still struggling later, I’d be happy for you to send me a copy of your code, and then I can take a look at it for you.

 Signature 

Remember the 8 Ps: Perfect Planning and Prior Preparation Prevents Piss-Poor Performance.


Not sure where to start with your project? Need some inspiration? Check out my CodeIgniter Resources thread

Profile
 
 
Posted: 29 June 2009 09:45 AM   [ Ignore ]   [ # 14 ]  
Grad Student
Avatar
Rank
Total Posts:  37
Joined  06-07-2009

But there are no redirects in the test controller only the code you sent me. This is completely puzzling. I’m beginning to think this will turn out something pretty small and I’ll bash myself for missing in in the first place smile

I’m having some hunches and will try to follow those leads. If I gather some more info or clues to what’s happenning I’ll post them here.

Again thanks for all the help!

 Signature 

lose > hesitate

Profile
 
 
Posted: 29 June 2009 09:50 AM   [ Ignore ]   [ # 15 ]  
Sr. Research Associate
Avatar
RankRankRankRankRank
Total Posts:  3896
Joined  04-25-2008

Sorry, I’ve just realised that the information I gave you was duff. As soon as you provide an argument for the controller, it’s assumed to be a method, so it’s looking for a method named arg1, which of course doesn’t exist. Please see my updated code, and try that with the following URL:

http://yoursite.tld/index.php/test/func/arg1/arg2

I apologise for being a complete muppet…

 Signature 

Remember the 8 Ps: Perfect Planning and Prior Preparation Prevents Piss-Poor Performance.


Not sure where to start with your project? Need some inspiration? Check out my CodeIgniter Resources thread

Profile
 
 
   
1 of 2
1