Session object available only in one page |
[eluser]Alhazred[/eluser]
I'm creating a login system, that works, but after the login I want to store some data into the session object and make them available everywhere, but I can't. This is how I'm doing that. The login form sends username and password to the controller, which calls a model's function for the login. The model's function returns an array with the user's data I want to store (returns false if the login fails). Once I have these data inside a variable here is what I do Code: $this->session->set_userdata($userdata); //$userdata is the array returned by the login function Code: //the print_r() shows correctly all the data stored into the session object Code: <body> Why can't I read the session object's content? The session library is set to autoload inside the autoload.php file. EDIT: it is only the added $userdata to not be available reading the session object, from other pages, the standard data are available. The session object is shown like this Code: Array
[eluser]jpganz[/eluser]
you are having an error on creating your session I think, check if the session ID changes everytime you refresh your browser.
[eluser]Alhazred[/eluser]
Yes, it always changes. What do I have to check? These are my settings inside the config.php Code: $config['sess_cookie_name'] = 'crd_session';
[eluser]smash120[/eluser]
In my project I have the session loaded in the construct of my users controller and I am able to use it anywhere in my project I dont understand why you are not able to use it. Try removing it from the autoloader and load it in your controller to see if that works. These are my settings which are pretty close to what you have Code: $config['sess_cookie_name'] = 'ci_session';
[eluser]smash120[/eluser]
Are you getting this info from the database? This is what I am doing in my model class After checking username and password against database assuming they typed in the correct username and password Code: if ($q->num_rows) {
[eluser]Alhazred[/eluser]
From the login_success view, when I call the print_r() on the session object I get this Code: Array
[eluser]Alhazred[/eluser]
Since I've got 2 types of users with different attributes, I have 2 different tables for them, so my login works in this way: - a controller receives username and password from a form - this controller loads a model and calls a login() methods which checks for the pair user/pass in the 1st table, if no match is found it tries the other table. - when a match is found, the method returns an array with the data taken from the database The code with all the checks is a bit long, but basically what happen in case of success is Code: $query = "SELECT id,username,email,... FROM ".$this->config->item('tbl_utenti')." WHERE username=? AND password=?"; Code: $userdata = $this->User_model->login($user,$pass);
[eluser]smash120[/eluser]
Your code looks like it works correctly but for some reason your lossing your session data when you navigate away from the page. Have you tried Code: print_r($user_data) since it is being stored the data array |
Welcome Guest, Not a member yet? Register Sign In |