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();
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题