Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/kotlin/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在php中使用标头正确重定向?_Php - Fatal编程技术网

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

如何在php中使用标头正确重定向?,php,Php,我的代码- <?php error_reporting(E_ALL); ?> <?php session_start(); if(isset($_POST["subLogin"])) { $uname = trim($_POST["tbUser"]); $pass = $_POST["pasUser"]; if($uname == "") { he

我的代码-

<?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”);”这一行被注释掉,那么它就可以正常工作。任何解决方案???

在重定向后调用exit()

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

是否尝试退出()?添加
exit在所有
头()调用之后。