Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting a HTTP response code in PHP (under Apache)

Tags:

php

apache

Given the following two methods for setting a HTTP response code in PHP (specifically, under Apache):

Method 1:

http_response_code(404);

Method 2:

header("HTTP/1.0 404 Not Found");

My questions are:

  1. Aside from the fact that http_response_code is only available in PHP 5.4 or greater, what are the differences between the two approaches and why/when to use one over the other?
  2. Where does the Reason Phrase come from when using the first example? (I've checked and a Reason Phrase is generated from somewhere)
like image 716
sg- Avatar asked Mar 14 '15 23:03

sg-


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" );

How do you set a status code in HTTP response?

Methods to Set HTTP Status Code Sr.No. This method sets an arbitrary status code. The setStatus method takes an int (the status code) as an argument. If your response includes a special status code and a document, be sure to call setStatus before actually returning any of the content with the PrintWriter.

What is Http_response_code in PHP?

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

How to get HTTP response status code in java?

Response response = client . target( url ) . request() . get(); // Looking if response is "200", "201" or "202", for example: if( Arrays.

How to set a certain PHP HTTP response?

To set a certain PHP HTTP response, you should use http_response_code () function. Now, there are various ways to control these responses. Let's see them in a table below:

Why is HTTP_response_code() not working on my website?

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: I only tested it on Apache.

How do I create my own response codes?

You can't create your own response codes using this method, however you can using the header method. 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.

What are HTTP PHP functions?

HTTP PHP functions allow you to manipulate PHP HTTP response (the information sent by your browser to server) before outputting it. These functions are inbuilt into PHP core. As we have already covered, exchanging data on the Internet consists of one side (client) asking for specific data and the other (server) supplying the requested information.


1 Answers

Since I'm being downvoted into oblivion for no apparent reason, I've managed to answer this myself by scouring through the PHP source code. Hopefully this serves as a reference for anyone else trying to work this out.

The two methods are essentially functionally equivalent. 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. Note also that http_response_code is only available in PHP 5.4.0 and higher.

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 function.

  3. http_response_code is only available in PHP 5.4.0 and higher

like image 133
sg- Avatar answered Oct 30 '22 12:10

sg-