Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why I have to call 'exit' after redirection through header('Location..') in PHP?

You know that if you want to redirect an user in PHP you can use the header function:

header('Location: http://smowhere.com'); 

It is also well known that it is a good practice to put also an exit; after the header call, to prevent execution of other php code. So my question is: could the code after the header-location call be effectively executed? In which cases? Can a malicious user be able to completely ignore the header('Location..') call? How?

like image 870
Nicolò Martini Avatar asked Apr 30 '10 21:04

Nicolò Martini


People also ask

Should I use exit after header in PHP?

You definitely should. Otherwise the script execution is not terminated. Setting another header alone is not enough to redirect. exit always interrupts the current script (in your case "fileA").

What is redirect () in PHP?

PHP. Redirection allows you to redirect the client browser to a different URL. You can use it when you're switching domains, changing how your site is structured, or switching to HTTPS. In this article, I'll show you how to redirect to another page with PHP.

Which PHP method can be used to redirect a Web page onto a next location?

Answer: Use the PHP header() Function You can simply use the PHP header() function to redirect a user to a different page. The PHP code in the following example will redirect the user from the page in which it is placed to the URL http://www.example.com/another-page.php .


1 Answers

could the code after the header-location call be effectively executed?

Yes, always. The header is only a line of data asking the browser to redirect. The rest of the page will still be served by PHP and can be looked at by the client by simply preventing the header command from executing.

That is easy enough to do with a command-line client like wget, for example, by simply telling it not to follow redirects.

Bottom line: If you don't prevent it, PHP will send out the whole body even after a header call. That body is fully available to the recipient without any special hacking skills.

like image 87
Pekka Avatar answered Oct 07 '22 13:10

Pekka