登錄失敗后,我試圖重定向到同一頁面,但我被重定向到。/login.php這是我的代碼: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>登錄.php:?phprequire '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"); }}如果憑據正確,我將被重定向到user.php或admin.php很好,但如果它們是錯誤的,我應該被重定向到index.php
1 回答

翻翻過去那場雪
TA貢獻2065條經驗 獲得超14個贊
您的查詢只生成與用戶名、密碼和類型實際匹配的行。因此,只有在憑據正確的情況下才會運行 while 循環。您需要將重定向放在 index.php 之后。
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");
- 1 回答
- 0 關注
- 134 瀏覽
添加回答
舉報
0/150
提交
取消