简体   繁体   中英

Fatal error: Call to a member function login() on a non-object in

Got an issue with a web app that I've inherited as a project and unfortunately I can't trace the error. It seems that the model isn't being loaded but I could be wrong. Any help would be great.

code is:

public function login()
{

    //If request is post then user is trying to login, so process the login info
    if ($_SERVER['REQUEST_METHOD'] === 'POST') {

        //Run the model method ::login
        $login_successful = $this->User->login();

        // check login status
        if($login_successful) {
            // if YES, then move user to dashboard/index (btw this is a browser-redirection, not a rendered view!)
            header('location: ' . URL . 'passwords/index');
        } else {
            echo "Incorrect user / pass combination entered. Please try again.";
        }


    }

}

and the model function is:

public function login() {

    $username = $_POST['data']['User']['username'];
    $password = $_POST['data']['User']['password'];


    $bind = array(
        ":username" => "$username",
    );
    $result = $this->select("users", "username = :username", $bind);

    //Check the password returned from the db against the password entered
    if (Bcrypt::checkPassword($password, $result[0]['password']) == true) {
        Session::init();

        Session::set('user_logged_in', true);
        Session::set('user_id', $result[0]['id']);
        Session::set('user_name', $result[0]['username']);
        Session::set('user_permission', $result[0]['permission']);
        Session::set('user_role', $result[0]['role']);

        return true;
    } else {
        return false;
    }
}

also I've noticed that the controller and model both have a function called login.]

Thanks

The reason is $this->User is not a valid class instance.

Make sure that it is an object.

Try

var_dump($this->User);
die();
//Run the model method ::login
$login_successful = $this->User->login();

And you will see that there is no instance there.

What to do?

Find the place where you expect your model being initialized. Check, why it is not.

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM