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 $_SESSION
s. 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 'someone@gmail.com' (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.
Code Follows Below
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