CodeIgniter Forums
[solved]Passing values from a record in a session? - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forum-20.html)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forum-23.html)
+--- Thread: [solved]Passing values from a record in a session? (/thread-42917.html)



[solved]Passing values from a record in a session? - El Forum - 06-23-2011

[eluser]brucebat[/eluser]
Hey all,

Im wanting to store the following variables:

-username
-access_level
-user_id

Within an array that is stored in the session.

Right now I am storing my username by using the "post" function after a user logs in.

However I want to get all the info from the corresponding record in the database.

I have tried this:

Code:
$user_session = array (


'username' => $this->db->get(user,$query, 'username')

Is this correct way of going about it?


[solved]Passing values from a record in a session? - El Forum - 06-23-2011

[eluser]toopay[/eluser]
Suppose we are in a method which receive the $_POST variable, you easily can do...
Code:
function login()
{
   //...

   // after validating the username and password...
   $qry = $this->db->where('username', $this->input->post('username'))->get('your_user_table');
   // This will contain a record of related user
   $userdata = $qry->result_array();
   // Put that into session
   $this->session->set_userdata('user', $userdata);
}



[solved]Passing values from a record in a session? - El Forum - 06-27-2011

[eluser]brucebat[/eluser]
Thanks for your reply toopay.

Im still having problems with this code.

Code:
function authenticate()
    {        
        $this->load->model('login_model');
        $userlogin = $this->login_model->validate();
        
         if($userlogin) // if the user's credentials validated...
        {
            $userquery = $this->db->where('username', $this->input->post('username'))->get('user'); //retrieves all user info
        
        
            $se_data = $userquery->result_array(); //variable to store all user info
            $this->session->set_userdata('user', $se_data); //variable stored in session
            
            
            $message = "Login successfull you will now be redirected to the user page.";
            redirect('site/blog',$message);
        }
        else // incorrect username or password
        {
            $message = "Incorrect username or password";
            redirect('site/login', $message);
            
        }
    }


It seems to be compiling but my site is not responding to this session being created e.g. Navigaation menu changing and different links being displayed.

Sorry to be a pest!


[solved]Passing values from a record in a session? - El Forum - 06-27-2011

[eluser]cideveloper[/eluser]
Is the session autoloaded? Check your db after successful login and see if the array is in there.


[solved]Passing values from a record in a session? - El Forum - 06-27-2011

[eluser]brucebat[/eluser]
Yes it is autoloaded.

I dont understand what you mean by checking my database to find an array in there? Sorry

This was my original code but this only stored a variable "username" taken from post

Code:
if($query) // if the user's credentials validated...
        {
            $data = array(
                'username' => $this->input->post('username'),
                'is_logged_in' => true
            );
            $this->session->set_userdata($data);
            redirect('site/members_area');
        }

Want I want is once the user is found I want to store in my session their:

userID -
name -used for displaying in the user control panel
username - will be used for retrieving other info
access_level - to control what pages users can see


The table where this is all held in is called "user".

What I want is to take those columns from that table and store them in a session.

-----------

The older bit of code was working before but it is not sophisticated enough to allow me to implement my navigation and admin control panel that I want on the site.

Thanks for your time!


[solved]Passing values from a record in a session? - El Forum - 06-27-2011

[eluser]cideveloper[/eluser]
Ok. looking at the authenticate function, you should do this. In your validate function in the model you are returning either true or false based on whether the user is authenticated or not. If they are authenticated then you should set the session variables in the model.

In the validate function you are probably selecting a row based on the criteria in your post. If the row is there you are returning true. before you do your return set the session value.

Code:
$username = $this->input->post('username', true);
$password = $this->input->post('password', true);
$this->db->where('username', $username);
$this->db->where('password', $password);
$query = $this->db->get('user');

if ($query->num_rows() > 0)
{
    $row = $query->row();
    $userdata = array (
        'userID' => $row->userID,
        'name' => $row->name,
        'username' => $row->username,
        'access_level' => $row->access_level
    );
    $this->session->set_userdata('user', $userdata);
    return TRUE;
}
return FALSE;

Then in your controllers get the "user" session value

Code:
$data['user_session'] = $this->session->userdata('user');
$this->load->view('myview', $data);

and pass it to your views. In you views you will be able to access each variable as such

Code:
<?php echo $user_session['name'];?>
<?php echo $user_session['access_level'];?>



[solved]Passing values from a record in a session? - El Forum - 06-27-2011

[eluser]brucebat[/eluser]
Thanks:

Im still having problems with the logic, I try to echo some user session stuff or check they are set and its saying undefined variable. Or it is not doing anything.

login_model/validate()

Code:
function validate()
    {
        $username = $this->input->post('username', true);
        $password = $this->input->post('password', true);
        
        $this->db->where('username', $username);
        $this->db->where('password', $password);
        $query = $this->db->get('user');

        if ($query->num_rows() > 0)
        {
            $row = $query->row();
            $userdata = array
            (
                'userID' => $row-> user_id,
                'name' => $row-> first_name,
                'username' => $row-> username,
                'access_level' => $row-> access_level
            );
            
            
            $this->session->set_userdata('user', $userdata);
            return TRUE;
        }
        return FALSE;
    }


login controller

Code:
function authenticate()
    {    
    
        $this->load->model('login_model');
        $userlogin = $this->login_model->validate();
        
        if($userlogin) // if the user's credentials validated...
            {
                $data['user_session'] = $this->session->userdata('user');
                redirect('site/home', $data);
                
            }
        else // incorrect username or password
        {
            $message = "Incorrect username or password";
            redirect('site/index', $message);
            
        }
    }



control panel view

Code:
<?php
    
    
    //checks if session variable has been set
        if ( ! $this->session->userdata('name')) //not
        {
            echo "<p><b>Guest", '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;', anchor('site/login', 'Login') ,'&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;', anchor('site/register', 'Register'), '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;', date("H:i:s", time()),'</b></p>';
        }
        
        else
    
        {
            echo '<p><b>',$this->session->userdata('name'),'&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;', anchor('', 'Control Panel'),'&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;', anchor('login/logout', 'Logout') ,'&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;',date("H:i:s", time()),'</b></p>';
        }

    ?&gt;

I am really confused with all the documentation with:

set_userdata
$userdata
setuserdata

etc

This is all still very alien to me from what im used to in C++

Thanks for your time.


[solved]Passing values from a record in a session? - El Forum - 06-27-2011

[eluser]cideveloper[/eluser]
if you want to do it that way change the model to this.

Code:
function validate()
    {
        $username = $this->input->post('username', true);
        $password = $this->input->post('password', true);
        
        $this->db->where('username', $username);
        $this->db->where('password', $password);
        $query = $this->db->get('user');

        if ($query->num_rows() > 0)
        {
            $row = $query->row();
            $this->session->set_userdata('userID', $row->user_id);
        $this->session->set_userdata('name', $row->first_name);
        $this->session->set_userdata('username', $row->username);
        $this->session->set_userdata('access_level', $row->access_level);
            return TRUE;
        }
        return FALSE;
    }



[solved]Passing values from a record in a session? - El Forum - 06-27-2011

[eluser]brucebat[/eluser]
Thankyou very much,

I figured out what was the actual culprit of my problem, I forgot my md5 on the password

Code:
$this->db->where('password', md5($this->input->post('password')));

Thankyou for being patient with me and a great help.

Regards

Bruce


[solved]Passing values from a record in a session? - El Forum - 04-06-2012

[eluser]elektra[/eluser]
Hi I'm fairly new to codeigniter and I'm actually having the same problem but can't seem to figure out why. All I want is to retrieve few user data so I could echo them out when they logged in. Really need help and running out of time. Thank you.

Controller
Code:
$query = $this->User_model->validate_login();
  
   if($query)
   {
    $data['user_session'] = $this->session->userdata('user');
    redirect('users/dashboard', $data);  
   }
   else
   {
    redirect('users/index');
   }  
  }
Model
Code:
public function validate_login()
{
  $this->db->where('username', $this->input->post('username'));
  $this->db->where('password', sha1($this->input->post('password')));
  $query = $this->db->get('users');
  
  if($query->num_rows == 1)
  {
   $row = $query->row();
      $userdata = array (
    'name' => $row->name,
    'username' => $row->username,
    'email' => $row->email,
    'role' => $row->role
       );
   $this->session->set_userdata('user', $userdata);    
   return TRUE;
  }
   return FALSE;
}
View
Code:
&lt;?php if($this->session->userdata('logged_in') == TRUE): ?&gt;
     Welcome &lt;?php echo $this->session->userdata('name'); ?&gt;, &lt;?php echo anchor('users/logout', 'Logout'); ?&gt;
    &lt;?php else: ?&gt;
     &lt;?php echo anchor('users/login', 'Login'); ?&gt; /
     &lt;?php echo anchor('users/register', 'Register'); ?&gt;
    &lt;?php endif; ?&gt;