Welcome Guest, Not a member yet? Register   Sign In
Inserting user ID into session userdata
#1

[eluser]dmregister[/eluser]
I am trying to return the user ID into my userdata after a user logs in. I can store the username easily but I do not know how to get the id out of the db query without running it through a foreach.

The model selects all where $this->post->input(username) and $this->post->input(password). If affected row == 1, then it stores everything into an empty array called data. I can store the username by creating an array( username => $this->post->input(username)) and store this in the session.

The million dollar question is how to return the id from the sql in a form that I can store into the userdata of the session. if I try to insert it from the data array ($data['id'] or $data->idWink I get an error trying to access a non-object.

Thank you in advance for any advice.

Regards
-David
#2

[eluser]cideveloper[/eluser]
First thing. You should show more of your code. like how you did the query. I assume its something like this.

Code:
$this->db->where('username', $this->post->input(username));
$this->db->where('password', $this->post->input(password));
$query = $this->db->get('users');
if ($query->num_rows() == 1){
    $user_data = $query->row_array();
    unset($user_data['password']);
    $user_data['logged_in'] = true;
    $this->session->set_userdata($user_data);
}

From this code you can see that you are getting the entire row into an array. Then you are removing the password field because you dont want that stored in the session. Then adding a logged_in key to the array with a value of true. Then drop that whole array into the session.
#3

[eluser]dmregister[/eluser]
Thank you for the quick response. I do not have access to my code at this moment but I will post when I have a chance this afternoon. This is similar to what I do, I was missing the step where I just query the result into an array into the userdata.

Thank you again for the fast response, and I will post more when I get a chance. Sorry for no code.

Regards,
-David
#4

[eluser]dmregister[/eluser]
My Model is:
Code:
function validate(){
    
        $this->db->where('Username', $this->input->post('username'));
        $this->db->where('Password', md5($this->input->post('password')));
        $sql = $this->db->get('members');
        
        if($sql->num_rows == 1){
            
            return true;
        }
    
    }

My Controller is:
Code:
if($query)//if user's credentials validate
        {
            $data = array(
            
            'username' => $this->input->post('username'),
            'is_logged_in' => true,
            );
            
            $this->session->set_userdata($data);
            redirect('siteController/home');
        }else{
            $this->index();
}


I am going to update it to return the array of user info right into the userdata and drop the password.

Thank you again for the help.

Regards,
-David




Theme © iAndrew 2016 - Forum software by © MyBB