PHP标头错误

时间:2013-10-26 16:27:24

标签: php header warnings

我的PHP Web应用程序存在问题,涉及在整个程序文件中使用标头。更具体地说,我创建了一个名为“error.php”的文件,如下所示:

        <p><?php 
            $errorCode= $_GET['message'];

            switch($errorCode)
            {
                case "1":
                    echo"<br>Sorry, all input fileds except for apartment number must be completed.<br>";
                    break;
                case "2":
                    echo "<br>You have entered an invalid name format.<br>";
                    break;
                case "3":
                    echo "<br>Name update not successful.<br>";
                    break;
                case "4":
                    echo "<br>Phone number update not successful.<br>";
                    break;
                case "5":
                    echo "<br>You have entered an invalid phone number format.<br>";
                    break;
                case "6":
                    echo "<br>You have entered an invalid street addres format.<br>";
                    break;
                case "7":
                    echo "<br>Apartment number update not successful.<br>";
                    break;
                case "8":
                    echo "<br>City name update not successful.<br>";
                    break;
                case "9":
                    echo "<br>State name update not successful.<br>";
                    break;
                case "10":
                    echo "<br>Zip code update not successful.<br>";
                    break;
                case "11":
                    echo "<br>Email address update not successful.<br>";
                    break;
                case "12":
                    echo "<br>You have entered an invalid email address format.<br>";
                    break;
                case "13":
                    echo "<br>You have entered an invalid password format.<br>";
                    break;
                case "14":
                    echo "<br>Can not find what to update.<br>";
                    break;
                case "15":
                    echo "<br>You have entered an invalid apartment number.<br>";
                    break;
                case "16":
                    echo "<br>You have entered an invalid password format.<br>";
                    break;
                case "17":
                    echo "<br>You have entered an invalid email address format.<br>";
                    break;
                case "18":
                    echo "<br>User not found in the database.<br>";
                    break;
                case "19":
                    echo "<br>User ID not found in database. Login Failure.<br>";
                    break;
        case "20":
            echo "<br>Failed connection to the database.<br>";
            break;
                case "21":
                    echo "<br>Street Address update not successful.<br>";
                    break;
                case "22":
                    echo "<br>User already exists.<br>";
                    break;
                case "23":
                    echo "<br>Failed to create a new user account.<br>";
                    break;
                case "24":
                    echo "<br>You have entered an invalid address format.<br>";
                    break;
                default:
                    echo "<br>You have an unknown error.<br>";
            }
        ?>   </p>    

问题是应用程序中的其他地方我会使用如下语句: 标题( “位置:/view/error.php消息= 23”);  标题( “位置:/view/error.php消息= 21”);等等... 我现在意识到这是有问题的,因为我经常得到警告,

"Warning: Cannot modify header information - headers already sent by (output started at /home/content/47/12002447/html/model/userRegistrationAction.php:6)/home/content/47/12002447/html/model/userRegistrationAction.php on line 92"

我得到的很多都是指向同一个文件。我的问题是,有没有一种方法可以将页面引导到“error.php”,其方式类似于我目前使用的方式,但使用标题? 我不想使用“include(”error.php?message ='some#')“因为它引入了大量的开销。

我想提前感谢你们提供任何帮助

1 个答案:

答案 0 :(得分:0)

确保在任何输出之前调用'header'函数。换句话说,没有HTML或javascript可以比'header'函数更早地调用,否则它将失败并显示您描述的错误消息。作为一个好的规则,将所有“标题”函数调用放在文件中的“html”开放标记上方