Hướng dẫn php header作用

mjt at jpeto dot net

13 years ago

I strongly recommend, that you use

header[$_SERVER["SERVER_PROTOCOL"]." 404 Not Found"];

instead of

header["HTTP/1.1 404 Not Found"];

I had big troubles with an Apache/2.0.59 [Unix] answering in HTTP/1.0 while I [accidentially] added a "HTTP/1.1 200 Ok" - Header.

Most of the pages were displayed correct, but on some of them apache added weird content to it:

A 4-digits HexCode on top of the page [before any output of my php script], seems to be some kind of checksum, because it changes from page to page and browser to browser. [same code for same page and browser]

"0" at the bottom of the page [after the complete output of my php script]

It took me quite a while to find out about the wrong protocol in the HTTP-header.

Marcel G

12 years ago

Several times this one is asked on the net but an answer could not be found in the docs on php.net ...

If you want to redirect an user and tell him he will be redirected, e. g. "You will be redirected in about 5 secs. If not, click here." you cannot use header[ 'Location: ...' ] as you can't sent any output before the headers are sent.

So, either you have to use the HTML meta refresh thingy or you use the following:



Hth someone

Dylan at WeDefy dot com

14 years ago

A quick way to make redirects permanent or temporary is to make use of the $http_response_code parameter in header[].



The HTTP status code changes the way browsers and robots handle redirects, so if you are using header[Location:] it's a good idea to set the status code at the same time.  Browsers typically re-request a 307 page every time, cache a 302 page for the session, and cache a 301 page for longer, or even indefinitely.  Search engines typically transfer "page rank" to the new location for 301 redirects, but not for 302, 303 or 307. If the status code is not specified, header['Location:'] defaults to 302.

mandor at mandor dot net

16 years ago

When using PHP to output an image, it won't be cached by the client so if you don't want them to download the image each time they reload the page, you will need to emulate part of the HTTP protocol.

Here's how:

Chủ Đề