-1

I have a login for it displayed an error message if the username and password are incorrect but I would rather it displays a Bootstrap alert box. I have tried to change the code but get errors this is the working code.

<?php
 require('db.php');
 session_start();

 if (isset($_POST['username'])){

     $username = stripslashes($_REQUEST['username']); 
     $username = mysqli_real_escape_string($con,$username); 

     $password = stripslashes($_REQUEST['password']);
     $password = mysqli_real_escape_string($con,$password);


     $query = "SELECT * FROM `users` WHERE username='$username' and password='".md5($password)."'";
     $result = mysqli_query($con,$query) or die(mysql_error());
     $rows = mysqli_num_rows($result);
     if($rows==1){


         $_SESSION['username'] = $username;
         header("Location: login1.php"); 
         }else{
             echo '<center><div class="alert alert-warning">Login Error!!</div></center>';
             }
 }else{
?>

<h1>Log In</h1>
<form action="" method="post" name="login">
<input type="text" name="username" placeholder="Username" required /><br><br>
<input type="password" name="password" placeholder="Password" required /><br><br>
<input name="submit" type="submit" value="Login" />




<div class="alert alert-danger" id="failure" style="margin-top: 10px; display: none">
                         <strong>Does Not Match!</strong> Invalid Username Or Password
                     </div>
</form>
<br>
<p>Not registered yet? <a href='registration.php'>Register Here</a></p>



<?php } ?>


</body>```
Avinash Singh
  • 4,970
  • 8
  • 20
  • 35
  • **Never store passwords in clear text or using MD5/SHA1!** Only store password hashes created using PHP's [`password_hash()`](https://php.net/manual/en/function.password-hash.php), which you can then verify using [`password_verify()`](https://php.net/manual/en/function.password-verify.php). Take a look at this post: [How to use password_hash](https://stackoverflow.com/q/30279321/1839439) and learn more about [bcrypt & password hashing in PHP](https://stackoverflow.com/a/6337021/1839439) – Dharman Jun 04 '20 at 12:16

1 Answers1

0

You close PHP immediately after the else. You can solve it like this:

    else{
echo '

<h1>Log In</h1>
<form action="" method="post" name="login">
<input type="text" name="username" placeholder="Username" required /><br><br>
<input type="password" name="password" placeholder="Password" required /><br><br>
<input name="submit" type="submit" value="Login" />




<div class="alert alert-danger" id="failure" style="margin-top: 10px; display: none">
                         <strong>Does Not Match!</strong> Invalid Username Or Password
                     </div>
</form>
<br>
<p>Not registered yet? <a href='registration.php'>Register Here</a></p>



';
?>
Andrea
  • 77
  • 6