PHP Reference ยท Web and Security
PHP http_response_code() function
Reference for PHP http_response_code(): set the HTTP status (404, 500, 301...), read the current code, how it differs from header() and what it returns in the CLI.
http_response_code() sets or reads the HTTP status code of the current response, the number such as 200, 404 or 500 that tells browsers, search engines and API clients how the request went. PHP sends 200 unless you change it. A Location header also changes it to 302.
It is the clearest way to say "not found" or "forbidden" without writing a full HTTP/1.1 404 Not Found line yourself, because PHP fills in the reason phrase for the protocol version in use.
http_response_code(404); echo 'Page not found';
Try it
In a command-line run there is no web server, so the function behaves a little differently from a real request. These examples show that behavior and a safe wrapper you can reuse in web code.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with fgets(STDIN)), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with fgets(STDIN)), it gets the next line from this box.
Syntax
http_response_code(int $response_code = 0): int|bool
| Name | Description |
|---|---|
$response_code |
Optional. The status code to send, normally from 100 to 599. If omitted or 0, the function only reads the current code. |
Return value:
- With no argument: the current code, or
falseif PHP is not running under a web server (CLI). - With an argument: the previous code, or
trueif none was set before. - If headers were already sent, the call has no effect, raises a warning and returns
false.
Errors / Exceptions: none are thrown. Calling it after output has started raises a warning and changes nothing.
Usage notes
- Call it before any output, just like
header(). - The page body is still sent. Setting
404does not stop the script. Print your error page, thenexit. - Use real status codes for errors. A "not found" page returned with
200(a soft 404) confuses search engines and API clients. - Common codes:
200OK,201Created,204No Content,301and308permanent redirects,302and303temporary redirects,304Not Modified,400Bad Request,401unauthenticated,403forbidden,404not found,405wrong method,422validation failed,429too many requests,500server error,503unavailable. - Redirects need a
Locationheader as well:header('Location: /new', true, 301)sets both at once. - Alternative:
header('HTTP/1.1 404 Not Found')works too, but ties your code to a protocol version. Prefer this function. - Under HTTP/2 and HTTP/3 there is no reason phrase; only the numeric code matters.
- Frameworks let you set the status on a response object. The function is for plain scripts.
Related
header(),setcookie(),exit/die,$_SERVER- Lessons: HTTP headers and redirects