Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Best way to send HTTP response code in PHP

Tags:

php

From reading the php spec and other questions on Stack Overflow, I can see three ways of sending an HTTP response code from PHP:

header("HTTP/1.0 404 Not Found");
           ^      ^     ^
           A      B     C

header(" ", false, 404);
        ^     ^     ^
        C     D     B

http_response_code(404);
                    ^
                    B

A: Defines HTTP header
B: Response code
C: Message
D: To replace previous header or not

What is the difference between these and which one is the best to use? Is my understanding of the parameters correct?

Thanks,

Tugzrida.

like image 382
Tugzrida Avatar asked May 25 '15 07:05

Tugzrida


People also ask

How do I get HTTP response in PHP?

For PHP versions 4.0: In order to send the HTTP response code, we need to assemble the response code. To achieve this, use header() function. The header() function contains a special use-case which can detect a HTTP response line and replace that with a custom one. header( "HTTP/1.1 404 Not Found" );

What is Http_response_code in PHP?

The http_response_code() function sets or returns the HTTP response status code.

What is PHP status code?

Status codes are similar in that they give information about if a page has loaded successfully or not, and the root cause of any errors. PHP is a scripting language that can generate status-code data.

What is the best HTTP response code for successful post request?

2xx Status Codes [Success] Indicates that the request has succeeded. Indicates that the request has succeeded and a new resource has been created as a result. Indicates that the request has been received but not completed yet. It is typically used in log running requests and batch processing.


1 Answers

To answer your question about what is the difference, I found this comment in the PHP docs (thanks Steven):

http_response_code is basically a shorthand way of writing a http status header, with the added bonus that PHP will work out a suitable Reason Phrase to provide by matching your response code to one of the values in an enumeration it maintains within php-src/main/http_status_codes.h. Note that this means your response code must match a response code that PHP knows about. You can't create your own response codes using this method, however you can using the header method.

In summary - The differences between http_response_code and header for setting response codes:

  1. Using http_response_code will cause PHP to match and apply a Reason Phrase from a list of Reason Phrases that are hard-coded into the PHP source code.

  2. Because of point 1 above, if you use http_response_code you must set a code that PHP knows about. You can't set your own custom code, however you can set a custom code (and Reason Phrase) if you use the header method.


I was curious about how some popular frameworks send the header in a standard response:

Symfony (and Laravel, by inheritance) sets the raw header:

// status
header(sprintf('HTTP/%s %s %s', $this->version, $this->statusCode, $this->statusText), true, $this->statusCode);

Zend Framework 2 also sets the raw header:

public function renderStatusLine()
{
    $status = sprintf(
        'HTTP/%s %d %s',
        $this->getVersion(),
        $this->getStatusCode(),
        $this->getReasonPhrase()
    );
    return trim($status);
}

And so does Yii

protected function sendHeaders()
{
    if (headers_sent()) {
        return;
    }
    $statusCode = $this->getStatusCode();
    header("HTTP/{$this->version} $statusCode {$this->statusText}");
    // ...
like image 60
Jody Avatar answered Sep 25 '22 19:09

Jody