I’m trying to redirect to the same page after login failed,but instead I get redirected to /login.php
.Here is my code:
Index.php:
<body> <h1><strong>Login</strong></h1> <form action="login.php" method="POST"> <table id="mytable"> <tr> <td><select name="type"> <option value="Admin">Admin</option> <option vale="User">User</option> </select></td> </tr> <tr> <td><input type="text" name="username" placeholder="Username"><br><br> </td> </tr> <tr> <td><input type="password" name="password" placeholder="Password"><br><br> </td> </tr> <tr> <td><input type="submit" value="Login" ><br><br> </td> </tr> </table> </form> </form> </body> </html>
Login.php:
?php require 'ceva.php'; $type=$_POST['type']; $username=$_POST['username']; $password=$_POST['password']; //if(!empty($username) && !empty($password)) //{ $sql= "SELECT * FROM login WHERE username ='$username' and password='$password' and type='$type'"; // selecteaza din baza de date login $rezultat= mysqli_query($con, $sql); while($rand=$rezultat->fetch_assoc()) { if($rand['username'] == $username && $rand['password'] == $password && $rand['type'] == 'Admin') //verificare array { header("Location: admin.php"); //muta in pagina admin.php }else if ($rand['username'] == $username && $rand['password'] == $password && $rand['type'] == 'User'){ header("Location: user.php"); }else { header("Location: index.php"); } }
If credentials are correct I get redirected to user.php
or admin.php
just fine,but if they are wrong I should be redirected to index.php
Advertisement
Answer
Your query only produces rows, that actually match username, password and type. So the while loop only runs if the credentials are correct. You need to put the redirect to index.php after it.
while($rand=$rezultat->fetch_assoc()) { if($rand['username'] == $username && $rand['password'] == $password && $rand['type'] == 'Admin') //verificare array { header("Location: index.php");header("Location: admin.php"); //muta in pagina admin.php exit; }else if ($rand['username'] == $username && $rand['password'] == $password && $rand['type'] == 'User'){ header("Location: user.php"); exit; } } header("Location: index.php");