Can't set response code to 200 in PHP
I'm trying to set the response code in a PHP script to 200. Later I set a Location
header, which sets it to 302. According to the documentation:
Not only does it send this header back to the browser, but it also returns a REDIRECT (302) status code to the browser unless the 201 or a 3xx status code has already been set.
Is there no way to set the header to 200 and not have the location
header override it? I've tried setting the response code header after the location header, but that doesn't work (presumably because the location header has already been sent along with the body including the "Found" HTML bit).
I could use 201 as the response code, since the application accecssing the script only checks for any 2xx code. However, according to the HTTP 1.1 spec:
The origin server MUST create the resource before returning the 201 status code.
But I'm not creating anything. Is that开发者_如何学编程 a problem, or can I get away with it?
According to RFC 2616, a 200 status code WITH a redirect is not valid (see 10.3 Redirection 3xx). When redirecting, only a 3xx code is valid.
You can continue to send an arbitrary 2xx code if it is getting the job done, but this would be outside the standard and theoretically not reliable across all user agents.
Well, I found an answer to my particular problem. My original problem was that one place needed the redirect
header and another place needed the HTTP/1.1 200 OK
header, so I was able to use one or the other depending on the source.
However, after trying the 201 option, that didn't work either, (still 302) so I don't know if that's a bug or if I was doing it wrong or what.
The "Location: " argument in header()
is intended for a redirect. A redirect is an instruction for the HTTP requester (i.e. the user-agent) to send a new request to the server with the information given in "Location".
Setting a status code with 200 at any time therefore is an error and has to be disregarded; which should default to 302.
If you don't want to redirect then simply don't set the location!
精彩评论