I have been following a tutorial on youtube about CodeIgniter. The tutorial only relies on $username $_SESSION variable and doesn't touch on other aspects of $_SESSIONs. According to code igniter documentation regarding $_SESSION you can simply call the session super global with the key. This is where my problem starts.
VAR_DUMP($_SESSION)
a var_dump of $_Session provides the following info (I have truncated other fields to keep it shorter)
array (size=5)
'__ci_last_regenerate' => int 1526800056
'' => null
'userID' =>
array (size=1)
0 =>
array (size=14)
'userID' => string '1' (length=1)
'firstname' => string 'Tim' (length=3)
'lastname' => string 'Coetzee' (length=7)
'pword' => string 'xyz' (length=4)
'email' => string '[email protected]' (length=17)
What I want to do
I simply want to be able to call a $_SESSION super global as such $_SESSION['email'] or $_SESSION['userID'] The way I want to access the session vars is EXACTLY as you should do it according to docs, thus I believe my
problem is in my controller() or the way im setting the session data() as can be viewed below
Problem
Lets say I want to display the userID from above var_dump() info in the view just as a test I do the following... echo $_SESSION['userID'];
The above leads to error
Message: Object of class stdClass could not be converted to string
Okay sure enough however what is the object name I should call to get say firstname ?
I tried this as per official docs
echo $this->session->firstname AND echo $this->user_data('userID');
which resulted in same error.
I realize the $_SESSION data seems to be saved in a multi-dimensional array so I tried a foreach() as such
foreach ($_SESSION as $sessions)
{
foreach ($sessions as $value)
{
echo $value;
}
}
which gives me the error:
Message: Invalid argument supplied for foreach()
However after two more similar errors by the last iteration it returns the values of all the sessions, so Im guessing im doing something wrong in the loop.
Users_model.php
public function login($username, $password)
{
// Validate
$this->db->where('username', $username);
$this->db->where('pword', $password);
$stmnt = $this->db->get('users');
if($stmnt->num_rows() == 1){
return $stmnt->row(1);
} else {
return false;
}
}
Controller.php
public function login(){
$data['title'] = 'Sign In';
$this->form_validation->set_rules('username', 'Username', 'required');
$this->form_validation->set_rules('password', 'Password', 'required');
if($this->form_validation->run() === FALSE){
$this->load->view('templates/header');
$this->load->view('users/login', $data);
$this->load->view('templates/footer');
} else {
// Get username
echo $username = $this->input->post('username');
// Get and encrypt the password
echo $password = $this->input->post('password');
// Login user
$user_id = $this->users_model->login($username, $password);
if($user_id){
// Create session
$user_data = array(
'username' => $username,
'userID' => $user_id,
'logged_in' => true
);
$username = user_data ['username'];
$userID = user_data ['userID'];
$logged_in = user_data ['logged_in'];
$this->session->set_userdata($username );
$this->session->set_userdata($userID );
$this->session->set_userdata($logged_in);
// Set message
//$this->session->set_flashdata('user_loggedin', 'You are now logged in');
redirect('pages/index');
} else {
// Set message
// $this->session->set_flashdata('login_failed', 'Login is invalid');
redirect('users/failed');
}
}
}
$config.php
$autoload['libraries'] = array('form_validation','session', 'pagination');
Any help advice, or constuctive criticism appreciated
Hope this will help you :
if you don't want to change your model query set session like this
$user = $this->users_model->login($username, $password);
if($user)
{
$user_data = array(
'username' => $username,
'userID' => $user->id,
'logged_in' => true
);
$this->session->set_userdata($user_data);
//$this->session->set_flashdata('user_loggedin', 'You are now logged in');
redirect('pages/index');
}
else
{
// Set message
// $this->session->set_flashdata('login_failed', 'Login is invalid');
redirect('users/failed');
}
print individual session key like this :
echo $this->session->userdata('username');
echo $this->session->userdata('userID');
echo $this->session->userdata('logged_in');
and whole session like this :
print_r($this->session->userdata());
Note : for CI Version 3.1.8, access like this :
$username = $this->session->username;
$userid = $this->session->userID;
for more https://www.codeigniter.com/user_guide/libraries/sessions.html#retrieving-session-data
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With