Welcome Guest, Not a member yet? Register   Sign In
[SOLVED] Session userdata is being lost
#1

[eluser]zbrox[/eluser]
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.

Code:
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!
#2

[eluser]TheFuzzy0ne[/eluser]
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?
#3

[eluser]zbrox[/eluser]
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.
#4

[eluser]TheFuzzy0ne[/eluser]
And you're not calling session_start() or $this->session->sess_create() anywhere? Do all of your controllers work as you'd expect?
#5

[eluser]zbrox[/eluser]
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
#6

[eluser]TheFuzzy0ne[/eluser]
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:

Code:
&lt;?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:

Code:
string(4) "arg1"
string(4) "arg2"
#7

[eluser]zbrox[/eluser]
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?
#8

[eluser]zbrox[/eluser]
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
#9

[eluser]TheFuzzy0ne[/eluser]
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
#10

[eluser]zbrox[/eluser]
Thanks a lot for the help. I'll give a shout when I make the uri's work correctly.




Theme © iAndrew 2016 - Forum software by © MyBB