PHP CodeIgniter:登录404错误'试图获取财产'电子邮件'非对象'

时间:2018-04-08 13:01:53

标签: php codeigniter login

我最近开始关注PHP CodeIgniter的一些编码教程,我的登录和注册系统遇到了一些麻烦。注册工作完美,访问数据库和创建帐户,但我的登录代码存在问题,我无法解决这个问题。

我收到以下错误:

  

遇到严重错误:通知

     

消息:试图获取财产'电子邮件'非对象

     

文件名:controllers / Auth.php

     

行号:23

     

回溯:

     

文件:/opt/lampp/htdocs/application/controllers/Auth.php行:23   功能:_error_handler

     

文件:/opt/lampp/htdocs/index.php行:315功能:require_once

以下代码为:

    <?php

class Auth extends CI_Controller{

    public function login()
    {
        $this->form_validation->set_rules('username', 'Username', 'required');
        $this->form_validation->set_rules('password', 'Password', 'required|min_length[5]');
        if($this->form_validation->run() == TRUE) {

            $username = $_POST['username'];
            $password = md5($_POST['password']);

            //check user in db

            $this->db->select('*');
            $this->db->from('users');
            $this->db->where(array('username'=>$username, 'password'=>$password));
            $query = $this->db->get();

            $user = $query->row();

            if ($user->email){
                $this->session->set_flashdata("success", "You are logged in.");

                $_SESSION['user_logged'] = TRUE;
                $_SESSION['username'] = $user->username;

                //redirect to profile page

                redirect("user/profile", "refresh");
            } else {
                $this->session->set_flashdata("error", "NO such account exists");
                //redirect("auth/login", "refresh");
            }

        }       
        $this->load->view('login');
    }

public function register()
    {
        if(isset($_POST['register'])) {
            $this->form_validation->set_rules('username', 'Username', 'required');
            $this->form_validation->set_rules('email', 'Email', 'required');
            $this->form_validation->set_rules('password', 'Password', 'required|min_length[5]');
            $this->form_validation->set_rules('password', 'Confirm Password', 'required|min_length[5]|matches[password]');

            if($this->form_validation->run() == TRUE) {
                //echo 'form validated';

                //add user in database

                $data = array(
                    'username'=>$_POST['username'],
                    'email'=>$_POST['email'],
                    'password'=> md5($_POST['password'])
                );
                $this->db->insert('users', $data);

                $this->session->set_flashdata("success", "Your account has been registered");
                redirect("auth/register", "refresh");
            }
        }
        $this->load->view('register');
    }
} 

?>

任何帮助都将不胜感激,如果解决方案需要我用于本教程的任何代码,请告诉我。

谢谢。

1 个答案:

答案 0 :(得分:0)

当数据库中的结果对象没有返回任何行而因此返回空的stdClass时,您将得到那种错误,因此您将无法访问任何属性。这样做的正确方法是检查num_rows()是否等于1(例如,应该只有一个用户具有该名称和密码)。

public function login() {
    $this->form_validation->set_rules('username', 'Username', 'required');
    $this->form_validation->set_rules('password', 'Password', 'required|min_length[5]');
    if ($this->form_validation->run() == TRUE) {

        $username = $_POST['username'];
        /**
         * Do not md5 your passwords!
         * Use password_hash and password_verify!!
         */
        $password = md5($_POST['password']); // DO NOT MD5 your passwords!

        //check user in db
        // this should be in a model!
        $this->db->select('*');
        $this->db->from('users');
        $this->db->where(array('username' => $username, 'password' => $password));
        $query = $this->db->get();

        if ($query->num_rows() == 1) {
            $user = $query->row();
            $this->session->set_flashdata("success", "You are logged in.");

            $_SESSION['user_logged'] = TRUE;
            $_SESSION['username'] = $user->username;

            //redirect to profile page
            redirect("user/profile", "refresh");
        } else {
            $this->session->set_flashdata("error", "NO such account exists");
            //redirect("auth/login", "refresh");
        }
    }
    $this->load->view('login');
}

一般来说,在使用num_rows() > 0 result() .etc之前,您应养成检查row()的习惯。因为你永远无法100%确定将返回数据。

例如:

//model

function get_users() {
     $query = $this->db->get('users');
     if ($query->num_rows() == 0) {
          return null;
     }
     return $query->result();
}

// controller

function something() {
    $users = $this->model->get_users();
    if (is_null($users)) {
         exit('no users');
    }
    print_r($users);
}