Ryan Lewis
Ryan Lewis

Reputation: 3

PHP: Trying to access array offset on value of type null

Here is the line that the error is coming from...

If (password_verify($password, $user['password'])){}

Here is what's inside this of statement...

    $_SESSION['id'] = $user['id']; 
    $_SESSION['username'] = $user['username']; 
    $_SESSION['email'] = $user['email']; 
    $_SESSION['verified'] = $user['verified']; 
    $_SESSION['message'] = "Success!"; 
    $_SESSION['alert-class'] = "alert-success"; 
    header ('location: verification.php'); 
    exit(); 
} else { 
    $errors['login_fail'] = "Incorrect Username or Password"; 
} 

Upvotes: 0

Views: 626

Answers (1)

Michal Hynčica
Michal Hynčica

Reputation: 6144

You need to check if you've managed to load the user from DB. It looks like the function that is loading data from DB returns null when the user is not found.

You can change your condition to something like this:

if(!empty($user) && password_verify($password, $user['password'])) {
  // ... log in user
} else {
  // ... do something when password or user doesn't match
}

Upvotes: 1

Related Questions