簡體   English   中英

如何使用php中的標頭正確重定向?

[英]How to redirect correctly using header in 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“);” 此行被注釋掉,然后工作正常。 任何解決方案?

重定向后調用exit()。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM