You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

PHP CodeIgniter登录遇404错误及Trying to get property 'email' of non-object问题求助

解决CodeIgniter登录时"Trying to get property 'email' of non-object"错误

Hey there, let's break down this error you're hitting in your CodeIgniter auth system. That notice on line 23 of Auth.php means you're trying to access an email property on something that isn't an object—most likely a null value (because your database query didn't find a matching user) or an array (if you used the wrong query method to fetch data).

Let's walk through the most common fixes for this:

1. Check how you're fetching the user from the database

In CodeIgniter, when you query for a single user, you have two main options:

  • ->row(): Returns a single object (so you use $user->email to access properties)
  • ->row_array(): Returns an associative array (so you need $user['email'] instead)

If you accidentally mixed these up—like using row_array() but trying to access ->email—that's going to throw this notice. Double-check your query code.

2. Always add a check for existing users first

Even if you're using ->row() correctly, if your login credentials are wrong (invalid email or password), the query will return null. Trying to access $user->email on a null value is exactly what's causing this notice.

Add a simple existence check before accessing any user properties:

// Example query to get the user
$user = $this->db->where('email', $this->input->post('email'))
                 ->get('users')
                 ->row();

// First confirm the user exists, then verify password
if ($user && password_verify($this->input->post('password'), $user->password)) {
    // Safe to access $user->email here
    $this->session->set_userdata('user_email', $user->email);
    redirect('dashboard');
} else {
    // Handle invalid credentials
    $this->session->set_flashdata('error', 'Wrong email or password!');
    redirect('auth/login');
}

3. Verify your password handling logic

One common reason the query returns null is if you're comparing passwords incorrectly. Remember:

  • When registering, you should hash the password with password_hash() before saving it to the database.
  • When logging in, never compare the raw input password directly to the hashed value in the DB. Use password_verify() instead—this handles the salt automatically.

If you skip this and just do a where('password', $raw_password) in your query, it will never find a match, leaving $user as null and triggering the error.

Here's a complete, safe example of a login method for your Auth.php controller:

public function login() {
    // Get form inputs
    $email = $this->input->post('email');
    $password = $this->input->post('password');

    // Fetch user by email
    $user = $this->db->where('email', $email)->get('users')->row();

    // Validate user exists and password is correct
    if ($user && password_verify($password, $user->password)) {
        // Set login session data
        $this->session->set_userdata([
            'logged_in' => true,
            'user_id' => $user->id,
            'user_email' => $user->email
        ]);
        redirect('dashboard');
    } else {
        // Flash error message and redirect back to login
        $this->session->set_flashdata('login_error', 'Invalid email or password. Please try again.');
        redirect('auth/login');
    }
}

The key takeaway here is to always validate that your $user variable is a valid object before trying to access any of its properties. This not only fixes the notice but also makes your auth system more robust.

内容的提问来源于stack exchange,提问作者JDoxx

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.22 09:00:00