doubledee Posted June 18, 2012 Share Posted June 18, 2012 I have a nested IF-THEN-ELSE with different Redirect statements for each path. Would it be okay to just have one exit(); after my IF-THEN-ELSE instead of having one after *each* Redirect? Debbie Quote Link to comment https://forums.phpfreaks.com/topic/264361-where-to-exit-after-redirect/ Share on other sites More sharing options...
RobertP Posted June 18, 2012 Share Posted June 18, 2012 as long as it wont fire any unexpected code, then there should be no problems. Quote Link to comment https://forums.phpfreaks.com/topic/264361-where-to-exit-after-redirect/#findComment-1354763 Share on other sites More sharing options...
doubledee Posted June 18, 2012 Author Share Posted June 18, 2012 as long as it wont fire any unexpected code, then there should be no problems. Well, I've always been confused about this... How can you "exit" a script if you have already left it via redirect?! It seems like it is too late at that point, right?! Debbie Quote Link to comment https://forums.phpfreaks.com/topic/264361-where-to-exit-after-redirect/#findComment-1354764 Share on other sites More sharing options...
RobertP Posted June 18, 2012 Share Posted June 18, 2012 the header function sends the header. if the browser wants to obey that header is another question. it is possible to turn off the redirect headers for most browsers. don't rely on your visitors hardware. Quote Link to comment https://forums.phpfreaks.com/topic/264361-where-to-exit-after-redirect/#findComment-1354766 Share on other sites More sharing options...
doubledee Posted June 18, 2012 Author Share Posted June 18, 2012 the header function sends the header. if the browser wants to obey that header is another question. it is possible to turn off the redirect headers for most browsers. don't rely on your visitors hardware. So this should work, right? if ($articleFound){ // Redirect to Article. header("Location: " . BASE_URL . $_SESSION['returnToPage'] . "#boxComments"); }else{ // Redirect to Last Page. if (isset($_SESSION['returnToPage']) && ($_SESSION['returnToPage'] !== '/account/create.php')){ // Return to Last Page. header("Location: " . BASE_URL . $_SESSION['returnToPage']); }else{ // Go to Home Page. header("Location: " . BASE_URL . "/index.php"); } }//End of DETERMINE REDIRECT TYPE // End script. exit(); Thanks, Debbie Quote Link to comment https://forums.phpfreaks.com/topic/264361-where-to-exit-after-redirect/#findComment-1354769 Share on other sites More sharing options...
RobertP Posted June 18, 2012 Share Posted June 18, 2012 seems good yeah. Quote Link to comment https://forums.phpfreaks.com/topic/264361-where-to-exit-after-redirect/#findComment-1354780 Share on other sites More sharing options...
doubledee Posted June 18, 2012 Author Share Posted June 18, 2012 seems good yeah. Thank you, RobertP!! Debbie Quote Link to comment https://forums.phpfreaks.com/topic/264361-where-to-exit-after-redirect/#findComment-1354781 Share on other sites More sharing options...
kicken Posted June 18, 2012 Share Posted June 18, 2012 How can you "exit" a script if you have already left it via redirect?! Sending a location header for a redirect has no effect on how the script runs. All it does is queue up another header to be sent when the script does exit (or start sending output). PHP will happily continue to execute your script beyond the header call. As such, it is generally advisable to put an exit after your redirects to tell PHP to stop there rather than keep running the script. If you don't you may have vulnerabilities in your code that you don't notice because things might "appear" to work successfully. That said, putting your exit after a bunch of if statements that all send various redirects is fine. You just need to have it somewhere to prevent the remainder of the script from running and possibly doing bad things. Quote Link to comment https://forums.phpfreaks.com/topic/264361-where-to-exit-after-redirect/#findComment-1354829 Share on other sites More sharing options...
doubledee Posted June 18, 2012 Author Share Posted June 18, 2012 How can you "exit" a script if you have already left it via redirect?! Sending a location header for a redirect has no effect on how the script runs. All it does is queue up another header to be sent when the script does exit (or start sending output). PHP will happily continue to execute your script beyond the header call. As such, it is generally advisable to put an exit after your redirects to tell PHP to stop there rather than keep running the script. If you don't you may have vulnerabilities in your code that you don't notice because things might "appear" to work successfully. That said, putting your exit after a bunch of if statements that all send various redirects is fine. You just need to have it somewhere to prevent the remainder of the script from running and possibly doing bad things. Okay, sounds good. Thanks! Debbie Quote Link to comment https://forums.phpfreaks.com/topic/264361-where-to-exit-after-redirect/#findComment-1354967 Share on other sites More sharing options...
scootstah Posted June 19, 2012 Share Posted June 19, 2012 You seem to have this sorted but I figured I would leave this anyway. I like to use a function like the one below to handle redirects so that I know the script is always terminated. Plus, the shorthand is nice. function redirect($url, $type = 'location', $delay = 3) { if ($type == 'refresh') { header("refresh:$delay;url=$url"); } else { header("location:$url"); } exit; } // usage redirect('http://google.com'); // sends "location" header redirect('http://google.com', 'refresh', 1); // sends "refresh" header with a 1 second delay Quote Link to comment https://forums.phpfreaks.com/topic/264361-where-to-exit-after-redirect/#findComment-1355000 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.