This is a discussion on header & headers_sent BUG within the PHP General forums, part of the PHP Programming Forums category; Hello all: I am having a hard time with a small piece of code. I was wondering if someone may ...
|
|||||||
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
|
|||
|
Hello all:
I am having a hard time with a small piece of code. I was wondering if someone may be able to explain why the following code will not work... I have been scratching my head for a few hours now and I am stumped. <?php header("Location: http://someplace.com"); if( ! headers_sent()) header("Location: http://somplaceelse.com"); ?> So I would expect this piece of code to direct me to somplace.com. However it does not, and I always end up at somplaceelse.com. I have done a tcpdump to assist with debugging here is the output below. From the look of it the first header is getting ignored all toghether. Is there some way to force changes I made to the headers, that will make headers_sent return TRUE? Thanks, -James --- GET /tracking/test.php HTTP/1.1 Host: dev.www.someplace.com User-Agent: Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.0.0) Gecko/20020623 Debian/1.0.0-0.woody.1 Accept: text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,video/x-mng,image/png,image/jpeg,image/gif;q=0.2,text/css,*/*;q=0.1 Accept-Encoding: gzip, deflate, compress;q=0.9 Accept-Charset: ISO-8859-1, utf-8;q=0.66, *;q=0.66 Keep-Alive: 300 Connection: keep-alive Cookie: toolkitAccess=1 HTTP/1.1 302 Found Date: Wed, 23 Jul 2003 10:04:55 GMT Server: Apache/1.3.27 (Unix) mod_ssl/2.8.14 OpenSSL/0.9.6d PHP/4.3.1 mod_perl/1.27 X-Powered-By: PHP/4.3.1 Location: http://someplaceelse.com Keep-Alive: timeout=15, max=100 Connection: Keep-Alive Transfer-Encoding: chunked Content-Type: text/html --- |
|
|||
|
according to the php doc, online the headers are not affected by the
Output Buffer, so any functions that manipulate the OB should have no effect. flush I do not beleive will fix this issue. (* I have tested it and it did not *) Still need a good way to accomplish this. Thanks, James Ivo Fokkema wrote: >You might want to check whether or not your header output is getting >buffered. My suggestion is a flush() after the fist call. I'm not an expert >on this, it's just an idea. > >HTH > > >-- >Ivo Fokkema >PHP & MySQL programmer >Leiden University Medical Centre >Netherlands > >"James M. Luedke" <james@dod.net> wrote in message >news:3F1CD481.1060802@dod.net... > > >>Hello all: >> I am having a hard time with a small piece of code. I was wondering >>if someone may be able to explain why the following code will not work... >> >> >I > > >>have been scratching my head for a few hours now and I am stumped. >> >><?php >> >>header("Location: http://someplace.com"); >> >>if( ! headers_sent()) >> header("Location: http://somplaceelse.com"); >> >>?> >> >>So I would expect this piece of code to direct me to somplace.com. >>However it does not, and I always end up at somplaceelse.com. >> >>I have done a tcpdump to assist with debugging here is the output below. >> From the look of it the first header is getting ignored all toghether. Is >>there some way to force changes I made to the headers, that will make >>headers_sent return TRUE? >> >>Thanks, >> >>-James >> >>--- >>GET /tracking/test.php HTTP/1.1 >>Host: dev.www.someplace.com >>User-Agent: Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.0.0) >>Gecko/20020623 Debian/1.0.0-0.woody.1 >>Accept: >> >> >> >text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q= >0.8,video/x-mng,image/png,image/jpeg,image/gif;q=0.2,text/css,*/*;q=0.1 > > >>Accept-Encoding: gzip, deflate, compress;q=0.9 >>Accept-Charset: ISO-8859-1, utf-8;q=0.66, *;q=0.66 >>Keep-Alive: 300 >>Connection: keep-alive >>Cookie: toolkitAccess=1 >> >>HTTP/1.1 302 Found >>Date: Wed, 23 Jul 2003 10:04:55 GMT >>Server: Apache/1.3.27 (Unix) mod_ssl/2.8.14 OpenSSL/0.9.6d PHP/4.3.1 >>mod_perl/1.27 >>X-Powered-By: PHP/4.3.1 >>Location: http://someplaceelse.com >>Keep-Alive: timeout=15, max=100 >>Connection: Keep-Alive >>Transfer-Encoding: chunked >>Content-Type: text/html >>--- >> >> >> > > > > > |
|
|||
|
--- "James M. Luedke" <james@dod.net> wrote:
> I was wondering if someone may be able to explain why the following > code will not work... I have been scratching my head for a few hours > now and I am stumped. > > <?php > header("Location: http://someplace.com"); > > if( ! headers_sent()) > header("Location: http://somplaceelse.com"); > ?> > > So I would expect this piece of code to direct me to somplace.com. Your second call to header() uses the same header name, thus that header (Location) is replaced with http://someplaceelse.com. The code works as expected. If you expected your conditional statement to fail, you are probably confusing headers_sent() to mean something like have_i_used_the_header_function_yet(). The headers are all sent at once, regardless of how many times you use header(). This function helps people avoid receiving the headers already sent warning, and as long as you have no output yet in your script, you should still be able to make changes to the headers. Hope that helps. Chris ===== Become a better Web developer with the HTTP Developer's Handbook http://httphandbook.org/ |