0

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.

7
  • stackoverflow.com/… Commented Dec 19, 2017 at 4:04
  • What`s the error? Commented Dec 19, 2017 at 4:12
  • Cannot use object of type stdClass as array Commented Dec 19, 2017 at 4:17
  • In which part of the code? Commented Dec 19, 2017 at 4:18
  • Is it this> echo $this->session->userdata['loggedUserData']['id']; Commented Dec 19, 2017 at 4:21

1 Answer 1

0

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.

Sign up to request clarification or add additional context in comments.

6 Comments

when i change this it worked, thank you for all who try to help me
$loggedUserData = $this->Auth_model->checkUser($userArray);
$loggedUserData = json_decode(json_encode($loggedUserData), True);
@SL cLaY, Your last line is weird. Why encode then decode? Its an array, you don't have to do that.
@Alex its not a array i think its object, i want convert it into array,
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.