Codeigniter尝试在Login Controller

时间:2017-06-30 10:18:44

标签: php codeigniter

我是codeignter的新手。我正在尝试使用它进行登录注册。在检查if($user -> email)时,它给出了错误说法试图获取此行的非对象的属性。我怎么能摆脱它。我已成功进行注册部分,但在尝试登录时出错。控制器代码在这里,

public function home() {

    $this->load->helper('url');
    $this->load->library('form_validation');
    $this->load->library('session');

    $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']);

        $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 have successfully logged in");

            $_SESSION['user_logged'] = TRUE;

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

            redirect('profile', 'refresh');
        } else {

            $this->session->set_flashdata("Error", "No such account in database");
        }
    }
    $this->load->view('login');
}

感谢您的帮助。

1 个答案:

答案 0 :(得分:0)

$ user为空时收到错误表示找不到给定用户名和密码的记录。 使用

 if(!empty($user->email)) instead of if ($user->email)