note 125538 added to function.http-response-code
| From: | viaujoc at videotron dot ca | Date: | Tue, 24 Nov 2020 12:53:31 +0000 |
| Subject: | note 125538 added to function.http-response-code | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-215893@lists.php.net to get a copy of this message | ||
Do not mix the use of http_response_code() and manually setting the response code header because
the actual HTTP status code being returned by the web server may not end up as expected.
http_response_code() does not work if the response code has previously been set using the header()
function. Example:
<?php
header('HTTP/1.1 401 Unauthorized');
http_response_code(403);
print(http_response_code());
?>
The raw HTTP response will be (notice the actual status code on the first line does not match the
printed http_response_code in the body):
HTTP/1.1 401 Unauthorized
Date: Tue, 24 Nov 2020 13:49:08 GMT
Server: Apache
Connection: Upgrade, Keep-Alive
Keep-Alive: timeout=5, max=100
Transfer-Encoding: chunked
Content-Type: text/html; charset=UTF-8
403
I only tested it on Apache. I am not sure if this behavior is specific to Apache or common to all
PHP distributions.
----
Server IP: 45.112.84.4
Probable Submitter: 185.85.3.174 (proxied: 184.161.143.67)
----
Manual Page -- http://php.net/manual/en/function.http-response-code.php
Edit -- https://master.php.net/note/edit/125538
Del: integrated -- https://master.php.net/note/delete/125538/integrated
Del: useless -- https://master.php.net/note/delete/125538/useless
Del: bad code -- https://master.php.net/note/delete/125538/bad+code
Del: spam -- https://master.php.net/note/delete/125538/spam
Del: non-english -- https://master.php.net/note/delete/125538/non-english
Del: in docs -- https://master.php.net/note/delete/125538/in+docs
Del: other reasons-- https://master.php.net/note/delete/125538
Reject -- https://master.php.net/note/reject/125538
Search -- https://master.php.net/manage/user-notes.php