Will all code be executed after redirecting the header in PHP?

So, I know that the general rule is that after redirecting the header in PHP, you have to call exit () to avoid running additional code, but I want to know if you put the code after the redirect header, if it always starts?

I studied various ways to track referrals in Google Analytics and came across this post: Google Analytics Tips and Tricks - Tracking 301 Redirects in Google Analytics

He recommends doing something like this:

<? Header( "HTTP/1.1 301 Moved Permanently" ); Header( "Location: http://www.new-url.com" ); ?> <script type="text/javascript"> var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www."); document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E")); </script> <script type="text/javascript"> try { var pageTracker = _gat._getTracker("UA-YOURPROFILE-ID"); pageTracker._trackPageview(); } catch(err) {}</script> 

From the moment I always understood the header () function, it's up to the browser and it can trigger the redirect whenever it wants. Thus, there is no guarantee that JavaScript will actually start or end before redirects occur.

The PHP documentation for the header () function indicates the reason for exiting after redirecting to "make sure the code below does not execute when we redirect." This is not like they guarantee that all subsequent codes will work, it just might happen.

Despite this, I found another way to control tracking, but I wanted to see if I could find out exactly how header () worked in this situation.

Thanks for your help.

+2
redirect php header
Aug 30 '11 at 16:35
source share
5 answers

Using the header function in PHP only adds to the response headers returned by the server. It does not immediately send any data and does not immediately terminate the connection. Any code after calling header will be executed.

In particular, it is recommended that you add a response body even after 301 redirects, so that clients that do not support redirection also receive some descriptive response. Infact in accordance with the HTTP 1.1 specification Section 10.3.2 -

If the request method was not HEAD, the response object SHOULD contain a short hypertext note with a hyperlink to the new URI (s). If the status code 301 is received in response to a request other than GET or HEAD, the user agent MUST NOT automatically redirect the request if it cannot be confirmed by the user, as this can change the conditions under which the request was sent.

+3
Aug 30 2018-11-11T00:
source share

This is a race condition. Once the redirect header is sent to the browser, the browser closes the current connection and opens a new one for the redirect URL. Until this original connection is closed and Apache disables the script, your code will continue to run as before.

Theoretically, if there was a fast enough connection between the client and server, and there was no buffering anywhere in the pipeline, issuing the header will lead to the immediate completion of the script. In fact, it can be anywhere between now and never to start a shutdown.

+3
Aug 30 '11 at 16:39
source share

HTML after your location bar is not running inside PHP; it will work in the browser. Whether or not the Javascript you specified on this page must be executed in the browser; PHP has nothing to do with this.

For me, PHP documents assume that any PHP below header() will continue to work when sending a redirect. But it "works" in the PHP interpreter, dumping JS into the browser. There is no connection between what it says in the PHP docs and whether the JS is launched by the browser.

0
Aug 30 '11 at 16:41
source share

EDIT

Well, as Anupam Jane remarked, it seems like browsers aren’t disconnecting without receiving the response body, and that sounds reasonable. So I changed my mind

This is not like they guarantee the execution of all the following code

Exactly Rather, this is a warning if there is any reasonable code that should not be executed. For example, the personal content of the page. Thus, in addition to sending the header, you should also make sure that no confidential content has been sent, and the output looks like a pretty reliable solution. So, I would call it "make sure that the reasonable code below does not execute when we redirect."

Thus, there is no guarantee that JavaScript will actually start or end before redirects occur.

Exactly It seems like this has nothing to do with script execution, but rather, the browser will execute something after receiving a 3xx response. I think I'm going to check it out, but you can check it out too.

0
Aug 30 2018-11-11T00:
source share

I noticed that the code is still executing, and several headers based on if statements can cause a "redirect loop error." I'm used to adding die("Redirecting..."); after each header redirection and don't see a problem.

0
Feb 17 '14 at
source share



All Articles