0

I have an issue with my PHP web application that involves the use of headers throughout my program files. To be more specific, I created a file called "error.php" that looks like this:

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

THE PROBLEM is that somewhere else in the application I would use statements like the following: header("location:/view/error.php?message=23"); header("location:/view/error.php?message=21"); and so on... I now come to realize this is problematic because I constantly get Warnings like,

"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"

And I get plenty of them mostly pointing to the same file. My QUESTION is, is there a way that I can direct pages to "error.php" in a way similar to the way I am currently using but with using header? I do not want to use "include("error.php?message= 'some #')" because it introduces a ton of overhead.

I would like to thank you guys in advance for any help

1 Answers1

0

make sure you call the 'header' function BEFORE any output. In other words, no HTML or javascript can be called earlier than the 'header' function or it will fail with the error message you describe. as a good rule, put all 'header' function calls above the 'html' open tag in your files

Chris Wesson
  • 299
  • 1
  • 4