duanchi8836 2014-02-04 16:30
浏览 36
已采纳

如何在PHP中使用标头正确重定向?

My code -

<?php
    error_reporting(E_ALL);
?>
<?php
    session_start();    

    if(isset($_POST["subLogin"]))
    {
        $uname = trim($_POST["tbUser"]);
        $pass = $_POST["pasUser"];
        if($uname == "") 
        {
            header('Location: index.php?e=1');
        }
        else if($pass == "") 
        {
            header('Location: index.php?e=2');
        }
        $unameF = "";
        $passF = "";    

        $userfile = fopen("DB/userDB.txt", "r");
        while(!feof($userfile))
        {    

            $arr1 = explode(":", fgets($userfile));
            $unameF = trim($arr1[1]);
            $arr1 = explode(":", fgets($userfile));
            $passF = trim($arr1[1]);
            $arr1 = explode(":", fgets($userfile));
            $type = trim($arr1[1]);
            if($unameF == $uname)
            {
                $_SESSION["uname"] = $uname;
                $_SESSION["type"] = $type;
                header('Location: welcome.php');
            }
        }
        header("Location: index.php?e=0");
    }
    else
    {
        header('Location: index.php');
    }
?>

Here, "header("Location: index.php?e=0");" always gets executed even if the username matches. And "welcome.php" is never reached. If "header("Location: index.php?e=0");" this line is commented out, then it works fine. Any solutions??

  • 写回答

1条回答 默认 最新

  • dsgdfh302506 2014-02-04 16:34
    关注

    Call exit() after your redirect.

    header('Location: index.php?e=1');
    exit();
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?