Danik Danik - 5 months ago 41
PHP Question

PHP displaying error code once

When a user clicks login, "Wrong Email Or Password" Is displayed for each user that is in Database Array. Could someone point me in the right direction to Only Display Error code once if user input wrong email/password?
I'm Using Flintstone to store Users.

if (isset($_POST['login'])) {
$TempEmail = strtolower($_POST['email']);
$TempPass = $_POST['password'];
// Get Keys From DB;
$keys = $users->getKeys();
// Check If DB is Empty
if (!empty($keys)) {
foreach ($keys as $key) {
$user = $users->get($key);
$email = strtolower($user['Email']);
$password = $user['Password'];
$hash = password_verify($TempPass, $password);
try {
if (($TempEmail === $email) && ($hash === true))
// On Successful Login redirects to home page
echo "Wrong Email Or Password";
} catch (Exception $e) {
return $e->getMessage();
} else {
echo "DB Is Empty";

Answer Source

Simply move echo "Wrong Email Or Password" right after the foreach-loop instead of inside it. If the email is found and the password matches, the user will still be redirected before the code reaches that point.

foreach ($keys as $key) {
    // Your current code, minus the echo.

// This is after the foreach and will only be executed if there were
// no match for the email and password.
echo "Wrong Email Or Password";

Just remember to add an exit; after your header('Location:....'); to stop PHP from executing anything more.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download