-2

I have this method in my class:

public function login ( $username, $password )
    {
            $pdo = parent::getConnection();
            $sql = 'SELECT username, pass FROM users
                WHERE username = :username';
            $stmt = $pdo->prepare($sql);
            $stmt->bindParam(':username', $username);
            $stmt->execute();

            if ( $stmt->rowCount() === 1 ) {
                $result = $stmt->fetch(PDO::FETCH_ASSOC);

                if (password_verify($password, $result['password'])) {
                    return true;
                } else {
                    Error::setError('', 'Wrong username or password');
                    return false;
                }
            }else {
                Error::setError('', 'Wrong username or password');
                return false;
            }
    }

I already try to use Isset like this, but that undefined index is still remaining:

if (isset($password)){
                    if (password_verify($password, $result['password'])) {
                        return true;
                    } else {
                        Error::setError('', 'Wrong username or password');
                        return false;
                    }
 }

when I use print_r to see my variable, it has a value. $username is from $_POST['usename'] and $password is from $_POST['password']

if($_SERVER['REQUEST_METHOD'] === 'POST'){
    $inputText = [
        'username',
        'password',
    ];

    $v->required($inputText);

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


    if (count(Error::getError())=== 0 && ($user->login($username,$password))){
        http_redirect(admin/index.php);
    }

}
Ying
  • 1,282
  • 4
  • 19
  • 34
  • possible duplicate of [PHP: "Notice: Undefined variable" and "Notice: Undefined index"](http://stackoverflow.com/questions/4261133/php-notice-undefined-variable-and-notice-undefined-index) – andrewsi Mar 19 '15 at 02:38

1 Answers1

0

You are selecting pass from the database but you are referencing password in your $result array.

The culprit:

if (password_verify($password, $result['password'])) {

The solution:

if (password_verify($password, $result['pass'])) {
Scopey
  • 6,269
  • 1
  • 22
  • 34