Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP: Effect of code after header("Location: abc.html")

Tags:

http

php

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

like image 886
check123 Avatar asked Apr 29 '11 15:04

check123


People also ask

What does header location do in PHP?

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.

Why is my header location not working in PHP?

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.

What does the function header sent in PHP return?

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.

What is content type header in PHP?

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.


1 Answers

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.

like image 187
Matthieu Napoli Avatar answered Sep 30 '22 10:09

Matthieu Napoli