Welcome Guest, Not a member yet? Register   Sign In
Store id from user in session
#1

[eluser]sb05[/eluser]
Hope someone can me help me with this one, been at it for a couple of days now.

I want to store the id (from the user that is logging in) in to the session.
I already added a row named 'id' in my ci_sessions table in the database.

I tried the following in the Session.php:
Code:
$this->userdata = array(
                            'session_id'     => md5(uniqid($sessid, TRUE)),
                            'ip_address'     => $this->CI->input->ip_address(),
                            'user_agent'     => substr($this->CI->input->user_agent(), 0, 50),
                            'last_activity'    => $this->now
                            );
                                                        if (isset($this->userdata['id'])) {
                                                        $db_data['id'] = $this->userdata['id'];
                                                         }
                                                         else {
                                                                $db_data['id'] = '';
                                                            }

Didn't have any effect i guess because in the database it saves as 0 everytime.

Thanks in advance
#2

[eluser]InsiteFX[/eluser]
Code:
// all of this is handled by the Session Libray
// no need to do any of this!
$this->userdata = array(
'session_id' => md5(uniqid($sessid, TRUE)),
'ip_address' => $this->CI->input->ip_address(),
'user_agent' => substr($this->CI->input->user_agent(), 0, 50),
'last_activity' => $this->now
);
// you do not need any of the above!

// set the users id
$this->session->set_userdata('id', $id);

// to get the user id
$id = $this->session->userdata('id');

if (isset($id)) {
$db_data['id'] = $this->userdata('id');
}
else {
$db_data['id'] = '';
}

I think you need to read the CodeIgniter USer Guide on Sessions.

CodeIgniter User Guide - Session Class

InsiteFX
#3

[eluser]sb05[/eluser]
This was originally in the Session.php.
Code:
function sess_create()
    {
        $sessid = '';
        while (strlen($sessid) < 32)
        {
            $sessid .= mt_rand(0, mt_getrandmax());
        }

        // To make the session ID even more secure we'll combine it with the user's IP
        $sessid .= $this->CI->input->ip_address();

        $this->userdata = array(
                            'session_id'     => md5(uniqid($sessid, TRUE)),
                            'ip_address'     => $this->CI->input->ip_address(),
                            'user_agent'     => substr($this->CI->input->user_agent(), 0, 50),
                            'last_activity'    => $this->now
                            );

Only thing i added where the lines:

Code:
if (isset($this->userdata['id'])) {
             $db_data['id'] = $this->userdata['id'];
         } else {
             $db_data['id'] = '';
             }

But it don't save the id of the user that is logging in.
I'm using the dx_auth library if that matters.

Tnx




Theme © iAndrew 2016 - Forum software by © MyBB