Lets say, the code looks something like this:
if(!$test) {
header("Location: somefile.html");
...some PHP code....
header("Location: anotherfile.html");
}
Is 'some PHP code' above executed? If yes, then what happens to further HTTP response(s) therein (eg.: the second 'header' statement in the code)?
The header in PHP is a PHP built-in function for sending a raw HTTP header. The HTTP functions are those that manipulate information sent by the webserver to the client or browser before it sends any further output. The header() function in PHP sends a raw HTTP header to a client or browser.
The root cause of this error is that php redirect header must be send before anything else. This means any space or characters sent to browser before the headers will result in this error. Like following example, there should not be any output of even a space before the headers are sent.
The headers_sent() function is an inbuilt function in PHP which is used to determines whether the header is successfully sent or not. The headers_sent() function returns True if header sent successfully and False otherwise.
The Content-Type header is used to indicate the media type of the resource. The media type is a string sent along with the file indicating the format of the file. For example, for image file its media type will be like image/png or image/jpg, etc. In response, it tells about the type of returned content, to the client.
Yes - the code will be executed.
The header()
will configure the headers to be returned, not send them right away.
If there is no output between the 2 calls, then only the last one will be taken into account.
However, if you output anything before the second call, then the headers will be sent, and the second call will result in an error (headers already sent).
A classic mistake is : redirect and not exit()
right after that, which can cause security problems.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With