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

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


?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");

    }

}

如果凭据正确,我将被重定向到user.php或admin.php很好,但如果它们是错误的,我应该被重定向到index.php


倚天杖
浏览 116回答 1
1回答

翻翻过去那场雪

您的查询只生成与用户名、密码和类型实际匹配的行。因此,只有在凭据正确的情况下才会运行 while 循环。您需要将重定向放在 index.php 之后。while($rand=$rezultat->fetch_assoc())&nbsp;{&nbsp; &nbsp; if($rand['username'] == $username && $rand['password'] == $password && $rand['type'] == 'Admin') //verificare array&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; header("Location: index.php");header("Location: admin.php"); //muta in pagina admin.php&nbsp; &nbsp; &nbsp; &nbsp; exit;&nbsp; &nbsp; }else if ($rand['username'] == $username && $rand['password'] == $password && $rand['type'] == 'User'){&nbsp; &nbsp; &nbsp; &nbsp; header("Location: user.php");&nbsp; &nbsp; &nbsp; &nbsp; exit;&nbsp; &nbsp; }}header("Location: index.php");
打开App,查看更多内容
随时随地看视频慕课网APP