Welcome Guest, Not a member yet? Register   Sign In
Session object available only in one page
#1

[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
    
$data['message'] = lang('crd_auth_login_success');
$data['userdata'] = $userdata;
$this->load->view('authentication/login_success',$data);
Inside the login_success view I have
Code:
//the print_r() shows correctly all the data stored into the session object
<?php echo $message ?>
<pre>
&lt;?php print_r($this->session->all_userdata()); ?&gt;
</pre>
<a href="&lt;?php echo base_url() ?&gt;">home</a>
The last link takes me to the home page, where I have
Code:
&lt;body&gt;
<pre>
  &lt;?php print_r($this->session->all_userdata()); ?&gt;
</pre>
... other html elements correctly rendered ...
&lt;/body&gt;
Here, print_r() shows nothing, from the source code I can read <pre></pre> but there is nothing between those tags.

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
(
    [session_id] => c5cf5900982680c1c252521317e53347
    [ip_address] => 127.0.0.1
    [user_agent] => Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:13.0) Gecko/20100101 Firefox/13.0.1
    [last_activity] => 1341939077
    [user_data] =>
)
#2

[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.
#3

[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';
$config['sess_expiration']  = 7200;
$config['sess_expire_on_close'] = FALSE;
$config['sess_encrypt_cookie'] = TRUE;
$config['sess_use_database'] = TRUE;
$config['sess_table_name']  = 'crd_sessions';
$config['sess_match_ip']  = FALSE;
$config['sess_match_useragent'] = TRUE;
$config['sess_time_to_update'] = 300;
#4

[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';
$config['sess_expiration'] = 7200;
$config['sess_expire_on_close'] = TRUE;
$config['sess_encrypt_cookie'] = TRUE;
$config['sess_use_database'] = TRUE;
$config['sess_table_name'] = 'ci_sessions';
$config['sess_match_ip'] = TRUE;
$config['sess_match_useragent'] = TRUE;
$config['sess_time_to_update'] = 300;
#5

[eluser]Alhazred[/eluser]
Just tried, I got the same behaviour.
#6

[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) {
    $row = $q->row();
    $id = $row->id;
    $data = array('userid' => $row->id,
                  'username' => $row->username,
                  'user_level' => $row->user_level,
                  'validated' => True);
    $this->session->set_userdata($data);
    $data = array('last_log' => date("F j, Y, g:i A",$this->session->userdata('last_activity')));
            
    $this->db->update('users',$data,array('id' => $id));
    return $this->session->userdata;
}
#7

[eluser]Alhazred[/eluser]
From the login_success view, when I call the print_r() on the session object I get this
Code:
Array
(
    [session_id] => 83db0843349010dfd895797374736d71
    [ip_address] => 127.0.0.1
    [user_agent] => Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:13.0) Gecko/20100101 Firefox/13.0.1
    [last_activity] => 1341941917
    [user_data] =>
    [id] => 1
    [email] => [email protected]
    [username] => Admin
    [abilitato] => 1
    [regdate] => 2012-07-08
    [lastconn] => 2012-07-10
    [permesso] => 1
    [loggato] => aX0-dp2#_0Ft
)
So the data are correctly stored as the login succeeds, when from this page I go to any other, all the data under [user_data] disappears.
#8

[eluser]smash120[/eluser]
can you post your login code?
#9

[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=?";
$bind = array($username,$password);
$result = $this->db->query($query,$bind);
if($result->num_rows() == 1)
{
return $result->row_array();
}
The controller uses the data in this way
Code:
$userdata = $this->User_model->login($user,$pass);
if($userdata != FALSE) //means that $userdata contains the array with the user's data
{
$this->session->set_userdata($userdata);
    
$data = array(); //reset array $data used above for other things
$data['message'] = lang('crd_auth_login_success');
$data['user_data'] = $userdata;
$this->load->view('authentication/login_success',$data);
}
#10

[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




Theme © iAndrew 2016 - Forum software by © MyBB