PHP breaks code after redirect?

I have a very simple question ... In the php function, I redirect if the value returns FALSE, otherwise continuing the code.

flibbertigibbet() { $value = true_or_false(); if ($value == FALSE) { //redirect to another controller method } //the rest of the code } 

What happens after the redirect? Can the code break or run a little if you say .. redirection takes longer to load?

Is it good to use exit() after a redirect?

+4
source share
3 answers

The code will continue to run - not only when the redirect takes longer, but each time from nd to the end.

If you use exit() , it depends on whether you want the rest of the code to execute. You can set header() and send a new address, but you can still perform some actions after that - for example, updating the database or some other bits.

I usually think that it is best to do all the necessary updates and send header() right at the end of the page - it makes debugging a lot easier and more intuitive.

+4
source

Just setting the redirect header does nothing on its own. ALWAYS exit or die after setting the location header (unless you know exactly what you are doing)

+4
source

It really depends on what you mean by redirection. Yes, it will never run the rest of the code if it is something like:
header("Location: http://newurl.com");

Otherwise, if there is no good reason for not returning (infinite loops, you try to include content that takes too long and does not set a timeout, you use exit() ...), then the rest of the code will be launched, as soon as the if condition code completes.

+1
source

All Articles