I'm building a project in CodeIgniter
and trying to create a login
functionality.
I just created a session on logging and passed the logged in user's id to the dashboard, but it showing me an error. Here is my code.
My controller - login controller ---------
class Login extends CI_Controller {
public function __construct() {
parent::__construct();
$this->load->model('Auth_model','',true);
}
public function index(){
$this->load->view('login/index');
}
public function verifyUser(){
$userArray = array(
"email"=>$this->input->post("email"),
"password"=>$this->input->post("password"),
);
$loggedUserData = $this->Auth_model->checkUser($userArray);
var_dump($loggedUserData);
if(count($loggedUserData)>0){
// this is where we creating session
$this->session->set_userdata('loggedUserData',$loggedUserData);
//redirect('dashboard/index');
}else{
redirect('login');
}
}
public function logout(){
$this->session->sess_destroy();
redirect('login');
}
}
My Model - Auth_model ---------
class Auth_model extends CI_model{
public function __construct() {
parent::__construct();
}
public function checkUser($userArray){
$loggedUserData = $this->db->select('id')
->where('email',$userArray["email"])
->where('password',$userArray["password"])
->get('users');
return $loggedUserData->row();
// here we are going to check the user with db
}
}
My view - dashboard------------
class Dashboard extends CI_Controller {
public function __construct() {
parent::__construct();
$currentUserData = $this->session->userdata();
if(!isset($currentUserData["loggedUserData"])){
redirect('/login/');
}
}
public function index(){
//$this->load->view('dashboard/index');
echo $this->session->userdata['loggedUserData']['id'];
}
}
Please help me out.
I believe the error is happening because of this call in the model.
return $loggedUserData->row();
The method row()
returns and object, but you want an array. Change the above to
return $loggedUserData->row_array();
and the error will go away. Not to say everything will work.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.