|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2015-08-14 19:45 UTC] jeff at nd4c dot com
Description: ------------ Setting the Location header will set a status code of 202 to 302. From the online documentation: (http://php.net/manual/en/function.header.php) ... The second special case is the "Location:" header. 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. ... The auto redirect ignores status codes: 201 and 3xx, but it SHOULD also ignore status code: 202. Using the Location header for a 202 response is now the standard for a RESTful api. According to: http://restcookbook.com/Resources/asynchroneous-operations/ ...you can then issue at 202 (Accepted) response code. This informs the client that the request has been accepted and understood by the server, but the resource is not (yet) created. Send the temporary resource inside the Location header. Request: POST /blogs HTTP/1.1 <xml> blogdata </xml> Response: HTTP/1.1 202 Accepted Location: /queue/12345 ... Test script: --------------- header("HTTP/1.1 202 Accepted"); header('Location: /some/arbitrary/uri'); // This will change the status code to 302, but it shouldn't header('Location: /some/arbitrary/uri'); header("HTTP/1.1 202 Accepted", true, 202); // This works, always forcing the status code prevents the defaults redirect handling. Expected result: ---------------- Status code 202 should remain as 202 after a Location header is set. PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Sun Dec 07 16:00:01 2025 UTC |
After some more thought and research, I've again changed my mind. I do believe that there is a real bug with header() function. But the bug description is now different... header(): Setting the Location header will set the status code to 302 even if the status code has been passed as the third parameter with any previous header call. Test scripts: // This format would only // require knowing if we want to force the status code // Passing status code 202 as a "forced" status code header("HTTP/1.1 202 Accepted", true, 202); // This will still set the status code to 302, // even though the 202 was forced. header('Location: /some/arbitrary/uri'); // This format would require knowing both: // if we want to force the status code AND // also knowing when we are passing a header // that might change the status code // Passing status code 202 as a "forced" status code header("HTTP/1.1 202 Accepted", true, 202); // This will work as expected with a forced 202 status code. header('Location: /some/arbitrary/uri', false, 202); // Current solution: To force the status code as the last header. // This format would only require // knowing if we want to force the status code // This will still set the status code to 302 header('Location: /some/arbitrary/uri'); // Passing status code 202 as a "forced" status code as the last header header("HTTP/1.1 202 Accepted", true, 202); I believe that since settings header should be allowed in any order since the RFC on headers does not set any requirements on the order of setting headers. I also believe that passing in a Status Code as a forced status code, it should remain 'forced' and not be overwritten by any following header calls unless a status code is passed in as a new forced header. I also question the benefit of having a Location header automatically setting the status code to a 302. The setting of a Location header would always be done in a context where the program would know whether or not to set the status code. There is no need or even desire for a 'magic' side effect. These are typically frowned upon!! So, at this point, I now consider this a bug. I hope that helps.