I just upgraded my PHP Version from 5.6 to 7.2. I used count()
function in my login page, example:
if(!empty($_POST['username']) && !empty($_POST['password'])):
$records = $conn->prepare('SELECT id,username,password FROM users WHERE username = :username');
$records->bindParam(':username', $_POST['username']);
$records->execute();
$results = $records->fetch(PDO::FETCH_ASSOC);
$message = '';
if(count($results) > 0 && password_verify($_POST['password'], $results['password']) ){
$_SESSION['user_id'] = $results['id'];
header("Location: /");
} else {
$message = 'Sorry, those credentials do not match';
}
endif;
After searching, I found questions and answers similar to this one but they were related to WordPress that they modified some files, but I couldn't find a solution for it using Pure PHP.
PDO fetch
returns false on failure. So you need to check this case too:
if($results && count($results) > 0 && password_verify($_POST['password'], $results['password']) ){
$_SESSION['user_id'] = $results['id'];
header("Location: /");
} else {
$message = 'Sorry, those credentials do not match';
}
来源:https://stackoverflow.com/questions/51594817/php-7-2-warning-count-parameter-must-be-an-array-or-an-object-that-impleme