Trying to Access Array Offset on Value of Type Bool

Trying to access array offset on value of type bool in PHP 7.4

Easy with PHP ?? null coalescing operator

return $Row['Data'] ?? 'default value';

Or you can use as such

$Row['Data'] ??= 'default value';
return $Row['Data'];

PDO Trying to access array offset on value of type bool

You could do something like. However it does not protect against unsecure password nor timing attacks.

<?php
$nm = $_POST['nm'];
$pw = $_POST['pw'];

try{
$pdo = new PDO('mysql:host=localhost;dbname=gold-market_main', 'root', '');
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}catch(PDOException $e) {
echo "Connection failed: ".$e->getMessage();
die();
}

if($nm == null){
die("Feld darf nicht leer sein!")
} //ctype does not protect

$sql = $pdo->prepare("SELECT k_nutzername, k_passwort FROM kunden WHERE k_nutzername = ?;");
$sql->bindValue(1,$nm,PDO::PARAM_STR); //bind a value to a query, called parametrized queries, most secure way against SQL injection.
$sql->execute();
$row = $sql->fetch(PDO::FETCH_ASSOC);

if(!$row) { // if the username not exists
//header("Location: login_wrongUN.html");
print("nm wrong");
} elseif($row['k_passwort'] != $pw) {
//header("Location: login_wrongPW.html");
print("pw wrong");
} else {
header("Location: konto.html");
}
$pdo = null;
?>

Warning: Trying to access array offset on value of type bool

You problem is that get_user_hash method will return false when there is no such user with provided username.
You should consider to change your code to:

if(!empty($user) && $this->password_verify($password,$user['password']) == 1){


Related Topics



Leave a reply



Submit