3

I cannot find the right way to send a custom HTTP status code with PHP (for example: 454). I've tried using:

header('HTTP/1.1 454', true);
header('X-PHP-Response-Code: 454', true);

with no avail. Even if I get the X-PHP-Response-Code: 454 line in the response header, a HTTP/1.1 500 Internal Server Error is always added at the top of it, overriding the status code.

I've tried http_response_code() too.

Is there any way to send a custom non-standard HTTP status code with PHP?

Thanks!

0

1 Answer 1

3

The solution is to add the HTTP status description to the header. For example:

    header('HTTP/1.1 454 My Custom Status', true);

PHP discards non-standard HTTP codes when provided without their description.

Sign up to request clarification or add additional context in comments.

1 Comment

A description is not required; according to spec it can be empty. But the space after the status code is required.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.