What is Http?
As per Wikipedia definition… “The Hypertext Transfer Protocol (HTTP) is an application protocol for distributed, collaborative, hypermedia information systems. HTTP is the foundation of data communication for the World Wide Web. Hypertext is structured text that uses logical links (hyperlinks) between nodes containing text.”
Whenever a request is made to a server for any page (webpage) of any website an HTTP status code is the response to the request. A request is made when a search engine crawler (like Googlebot) crawls the page or a user tried to open the page in a web browser.
This status code provides the information about status of the request. Some of the common status codes are:
- 200 – The server successfully processed the request. Generally, this means that the server provided the requested page
- 301 – The page is permanently moved
- 404 – The requested page doesn’t exist
- 500 – Internal server error
HTTP Status codes are always three-digit numbers. The first digit of status code begins with one of five numbers, 1 through 5. From the 100s through the 500s, status codes are divided into following categories:
- 100s – Informational: Request has been received and the process is continuing
- 200s – Success: Request was received and processed successfully
- 300s – Redirection: Request has been received, but needs to perform an additional step to complete the request
- 400s – Client Error: Request was made by the client, but the page is not valid
- 500s – Server Error: Valid request was made by the client, but the server failed to complete the request
Below I have shared a few learning resources on HTTP Status Codes:
- Wikipedia: http://en.wikipedia.org/wiki/Hypertext_Transfer_Protocol
- IETF: http://www.ietf.org/assignments/http-status-codes/http-status-codes.xml
- W3C: http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html
- REST API: http://www.restapitutorial.com/httpstatuscodes.html
HTTP Status Code and PHP
PHP has a function http_response_code which gets or sets the HTTP Response / Status Code.
If you pass no parameters then http_response_code will get the current status code. If you pass a parameter it will set the response code.
The above code will first set the http status code to 404 and then print the current status code, i.e., 404.
Please note that this http_response_code was introduced in PHP 5.4.