Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Header use in PHP

Pretty simple question: which one of these two PHP (version 5+) header call is the "best"?

header('Not Modified', true, 304);
header('HTTP/1.1 304 Not Modified');

I'm pretty sure the first one is the most polyvalent one, but just curious if PHP would "fix" the second one if under HTTP 1.0...

Thanks!

Edit: One of these header crashes PHP on my Web host. Follow-up question at: PHP header() call "crashing" script with HTTP 500 error

like image 331
AlexV Avatar asked Jan 04 '10 15:01

AlexV


1 Answers

I would use this one:

header($_SERVER['SERVER_PROTOCOL'].' 304 Not Modified', true, 304);

$_SERVER['SERVER_PROTOCOL'] contains the protocol used in the request like HTTP/1.0 or HTTP/1.1.


Edit    I have to admit that my suggestion is senseless. After a few tests I noticed that if the first parameter is a valid HTTP status line, PHP will use that status line regardless if and what second status code was given with the third parameter. And the second parameter (documentation names it replace) is useless too as there can not be multiple status lines.

So the second and third parameter in this call are just redundant:

header($_SERVER['SERVER_PROTOCOL'].' 304 Not Modified', true, 304);

Use just this instead:

header($_SERVER['SERVER_PROTOCOL'].' 304 Not Modified');
like image 115
Gumbo Avatar answered Nov 11 '22 07:11

Gumbo