-2

I have been following OOP creating an login system. It was quite difficult for me but still pretty nice. I now came across a really rookie question but have no idea how to fix it, could someone help me with that?

$user_found = User::verify_user($username, $password);

if($user_found) {

    $session->login($user_found);
    redirect("index.php");

} else {
    $error = "Your password or username are invalid.";
}

I created a method to login the user under User class, and if the password or the username was wrong, an error message will pop out.

<div class="col-md-4 col-md-offset-2">

<h4 class="bg-danger"><?php echo $error; ?></h4>

<form id="login-id" action="login.php" method="post">

<div class="form-group">
<label for="username">Username</label>
<input type="text" class="form-control" name="username" value="<?php echo htmlentities($username); ?>" >

</div>
<div class="form-group">
<label for="password">Password</label>
<input type="password" class="form-control" name="password" value="<?php echo htmlentities($password); ?>">
</div>
<div class="form-group">
<input type="submit" name="submit" value="Submit" class="btn btn-primary">

</div>

However, when I wanted to echo $error, it kept saying that $error is an undefined variable. I don't know what's wrong and I don't know how to fix this. Can someone help me with that?

Thanks.

Roy Huang
  • 9
  • 3

1 Answers1

2

The $error will be set only if $user_found == false. Add a check -

<?php
if(!empty($error)) {
?>
   <h4 class="bg-danger"><?php echo $error; ?></h4>
<?php
}
?>
Sougata Bose
  • 31,517
  • 8
  • 49
  • 87