0

我的 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>    

问题是在应用程序的其他地方我会使用如下语句: header("location:/view/error.php?message=23"); header("位置:/view/error.php?message=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 #')" 因为它会带来大量开销。

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

4

1 回答 1

0

确保在任何输出之前调用“标题”函数。换句话说,不能在“header”函数之前调用任何 HTML 或 javascript,否则它将因您描述的错误消息而失败。作为一个很好的规则,将所有“标题”函数调用放在文件中的“html”打开标记之上

于 2013-10-26T16:46:43.963 回答