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.
redirect php header
David Stinemetze Aug 30 '11 at 16:35 2011-08-30 16:35
source share