   PHP redirect craziness
  Hi,Because I couldn't find anything better, I wrote a quick-and-simply PHP function to handle server-side page directs that my web app can incorporate into its business logic:function PhpRedirect($newUrl){$sub = SUB_DOMAIN;$website = WEBSITE;header("Location: http://$sub.$website/dts/$newUrl");}So, if SUB_DOMAIN = "www" and WEBSITE = "abc" then a call to PhpRedirect("bizarro.php") results with you being navigated away from the current page and to http://www.abc.com/bizarro.php.Hate it or love it, I tested it quickly and it was working fine.Until...You put two of these function calls back-to-back:PhpRedirect("dog.php");PhpRedirect("cat.php");You'd think that it would execute the first statement, redirecting you away from the (this) current page, preventing the second redirect from ever executing, right?Wrong! You end up at cat.php!The best I can surmise is that (and I'm a little weak on my HTTP theory), but perhaps headers are universal, and don't have anything to do with the document object (the page that is being construced).....?Anyways, please let me know if you have any ideas as to what is happening, and how I can change this.Edit: I realize now that header()s must be the first output in the file, else they dont work quite right. So I guess this post has become "how to redirect without header?" Google didnt turn up anything useful...Thanks,Z Harvey   
  In my redirect function, I call die immediately after calling header, which stops any more of the PHP script from executing (which would stop the "Location: cat" header info from being sent to the client).   ;Quote:Original post by plywoodEdit: I realize now that header()s must be the first output in the file, else they dont work quite right. So I guess this post has become "how to redirect without header?" Google didnt turn up anything useful...If you're really set on not using header(), you could use this instead, but you're still stuck with having to put it inside your <head> element.  I'd recommend just catching a redirect request at the very start of processing and using header() (calling die after a redirect as shown above).  Shouldn't be difficult, especially if you're using one PHP file as a "dispatch" for most of the content on your site (or including a common script in every directly-accessible PHP file _before_ sending anything to the client).   ;  Yup, you need to call "exit;" at the end of the function. This of course comes with a caveat: If your script needs to do any kind of file/database cleanup before finishing, calling exit inside the function may not be the most graceful thing to do. But for most purposes, it's just fine.Additionally, you should limit your script's output to the end of the script ( like with PrintPage() or similar) or otherwise tell the output buffer to wait until the script finishes before sending data to the client. This will let you kill the script early with a redirect and not have to worry about page contents you may have already started sending.   ;Quote:Original post by plywoodAnyways, please let me know if you have any ideas as to what is happening, and how I can change this.header() parses the header. They are stored in an associative array which you can query with getallheaders(). Earlier headers are overwritten and so only the last recent one is sent.   ;  Is there an actual reason for unconditionally redirecting twice in a row? I can't think of any scenario where this could be useful.   ;Quote:Original post by plywoodfunction PhpRedirect($newUrl){$sub = SUB_DOMAIN;$website = WEBSITE;header("Location: http://$sub.$website/dts/$newUrl");}...You put two of these function calls back-to-back:PhpRedirect("dog.php");PhpRedirect("cat.php");You'd think that it would execute the first statement, redirecting you away from the (this) current page, preventing the second redirect from ever executing, right?No. Remember how web servers work - they write out a large lump of text which they send to a web browser. That's all. What you've done here - and you can see this from the code above - is tell the server to write out 2 HTTP header lines, both changing the location. The server doesn't know or care that this is about redirection. It just does what you asked it to and adds those 2 header lines. Konfusius says above that it just sends one of the two, where they're duplicates, which is reasonable.Quote:but perhaps headers are universal, and don't have anything to do with the document object (the page that is being construced).....?The web server doesn't construct a document object. It writes out a lump of text, which is typically supposed to follow the HTTP standard. The first bit of the text will be the HTTP headers, and the rest of the text will be the body - this body is typically HTML. When this arrives on the client, the client reads the HTML and constructs a document object from it. But that's long after the server has sent it.   