Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP does the code break after a redirect?

I have a very basic question... In a php function I'm redirecting if a 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? Does the code break or does a little execute if say.. the redirect takes longer to load?

Is it good practice to use exit() after redirects?

like image 805
CyberJunkie Avatar asked Jan 16 '23 19:01

CyberJunkie


2 Answers

The code will continue to execute - not just when the redirect takes longer, but each time nd through to the end.

Whether you use exit() depends on whether or not you want the rest of the code to be executed. You might set a header() and send a new address, but you can still execute things afterwards - like updating a database or some other bits.

I normally think it is best to do all your required updating and send the header() right at the end of a page - makes debugging much easier and more intuitive.

like image 103
Fluffeh Avatar answered Jan 24 '23 23:01

Fluffeh


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

like image 37
Niet the Dark Absol Avatar answered Jan 24 '23 22:01

Niet the Dark Absol