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

时间:2014-02-04 16:30:33

标签: php

我的代码 -

<?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');
    }
?>

这里,“header(”Location:index.php?e = 0“);”即使用户名匹配,也始终执行。并且永远不会达到“welcome.php”。如果“header(”Location:index.php?e = 0“);”这条线被注释掉了,然后它运行正常。任何解决方案??

1 个答案:

答案 0 :(得分:1)

重定向后调用exit()。

header('Location: index.php?e=1');
exit();