登录失败后php重定向到同一页面

登录失败后php重定向到同一页面,php,html,mysql,Php,Html,Mysql,登录失败后,我试图重定向到同一页面,但我被重定向到/login.php。以下是我的代码: Index.php: <body> <h1><strong>Login</strong></h1> <form action="login.php" method="POST"> <table id="mytable"> <tr>

登录失败后,我试图重定向到同一页面,但我被重定向到
/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>

如果凭据正确,我会被重定向到
user.php
admin.php
很好,但如果凭据错误,我应该被重定向到
index.php

您的查询只会生成与用户名、密码和类型相匹配的行。因此,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");

旁注:最好添加
退出在每个头之后。您的代码对sql注入开放,并通过使用纯文本密码受到破坏。顺便说一句,您还有一个额外的
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");