Whether you're a programmer or not, you have seen it everywhere on the web. Even your first Hello World PHP script sent HTTP headers without you realizing it. In this article, we are going to learn about the basics of HTTP headers and how we can use them in our web applications. Show What Are HTTP Headers?HTTP stands for "Hypertext Transfer Protocol". The entire World Wide Web uses this protocol. It was established in the early 1990s. Almost everything you see in your browser is transmitted to your computer over HTTP. For example, when you opened this article page, your browser probably sent over 40 HTTP requests and received HTTP responses for each. HTTP headers are the core part of these HTTP requests and responses, and they carry information about the client browser, the requested page, the server, and more. ExampleWhen you type a URL in your address bar, your browser sends an HTTP request, and it may look like this: 1 GET /tutorials/other/top-20-mysql-best-practices/ HTTP/1.1 2 Host: code.tutsplus.com 3 User-Agent: Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.1.5) Gecko/20091102 Firefox/3.5.5 (.NET CLR 3.5.30729) 4 Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8 5 Accept-Language: en-us,en;q=0.5 GET /tutorials/other/top-20-mysql-best-practices/ HTTP/1.10 GET /tutorials/other/top-20-mysql-best-practices/ HTTP/1.11 GET /tutorials/other/top-20-mysql-best-practices/ HTTP/1.12 GET /tutorials/other/top-20-mysql-best-practices/ HTTP/1.13 GET /tutorials/other/top-20-mysql-best-practices/ HTTP/1.14 GET /tutorials/other/top-20-mysql-best-practices/ HTTP/1.15 GET /tutorials/other/top-20-mysql-best-practices/ HTTP/1.16 GET /tutorials/other/top-20-mysql-best-practices/ HTTP/1.17 GET /tutorials/other/top-20-mysql-best-practices/ HTTP/1.18 GET /tutorials/other/top-20-mysql-best-practices/ HTTP/1.19 20 21 22 23 The first line is the "Request Line", which contains some basic information on the request. And the rest are the HTTP headers. After that request, your browser receives an HTTP response that may look like this: 1 25 2 27 3 29 4 Host: code.tutsplus.com1 5 Host: code.tutsplus.com3 GET /tutorials/other/top-20-mysql-best-practices/ HTTP/1.10 Host: code.tutsplus.com5 GET /tutorials/other/top-20-mysql-best-practices/ HTTP/1.12 Host: code.tutsplus.com7 GET /tutorials/other/top-20-mysql-best-practices/ HTTP/1.14 Host: code.tutsplus.com9 GET /tutorials/other/top-20-mysql-best-practices/ HTTP/1.16 31 GET /tutorials/other/top-20-mysql-best-practices/ HTTP/1.18 33 20 35 22 37 38 39 User-Agent: Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.1.5) Gecko/20091102 Firefox/3.5.5 (.NET CLR 3.5.30729)0 User-Agent: Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.1.5) Gecko/20091102 Firefox/3.5.5 (.NET CLR 3.5.30729)1 User-Agent: Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.1.5) Gecko/20091102 Firefox/3.5.5 (.NET CLR 3.5.30729)2 User-Agent: Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.1.5) Gecko/20091102 Firefox/3.5.5 (.NET CLR 3.5.30729)3 User-Agent: Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.1.5) Gecko/20091102 Firefox/3.5.5 (.NET CLR 3.5.30729)4 User-Agent: Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.1.5) Gecko/20091102 Firefox/3.5.5 (.NET CLR 3.5.30729)5 User-Agent: Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.1.5) Gecko/20091102 Firefox/3.5.5 (.NET CLR 3.5.30729)6 User-Agent: Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.1.5) Gecko/20091102 Firefox/3.5.5 (.NET CLR 3.5.30729)7 User-Agent: Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.1.5) Gecko/20091102 Firefox/3.5.5 (.NET CLR 3.5.30729)8 User-Agent: Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.1.5) Gecko/20091102 Firefox/3.5.5 (.NET CLR 3.5.30729)9 40 41 42 43 44 45 46 The first line is the "Status Line", followed by "HTTP Headers", until the blank line. After that, the "content" starts (in this case, the HTML output). When you look at the source code of a web page in your browser, you will only see the HTML portion and not the HTTP headers, even though they actually have been transmitted together, as you can see above. These HTTP requests are also sent and received for other things, such as images, CSS files, JavaScript files, etc. That's why I said earlier that your browser sent at least 40 or more HTTP requests as you loaded just this article page. Now, let's start reviewing the structure in more detail. How to See HTTP HeadersI used Firefox Firebug to analyze HTTP headers, but you can use the Developer Tools in Firefox, Chrome, or any modern web browser to view HTTP headers. In PHP:
Further in the article, we will see some code examples in PHP. HTTP Request StructureThe first line of the HTTP request is called the request line and consists of three parts:
The remainder of the request contains HTTP headers as 315 pairs on each line. These contain various information about the HTTP request and your browser. For example, the 316 line provides information on the browser version and the Operating System you are using. 317 tells the server if your browser can accept compressed output like gzip. You may have noticed that the cookie data is also transmitted inside an HTTP header. And if there was a referring URL, that would have been in the header too. Most of these headers are optional. This HTTP request could have been as small as this: 1 GET /tutorials/other/top-20-mysql-best-practices/ HTTP/1.1 2 Host: code.tutsplus.com And you would still get a valid response from the web server. Request MethodsThe three most commonly used request methods are GET, POST, and HEAD. You're probably already familiar with the first two from writing HTML forms. GET: Retrieve a DocumentThis is the main method used for retrieving HTML, images, JavaScript, CSS, etc. Most data that loads in your browser was requested using this method. For example, when loading an Envato Tuts+ article, the very first line of the HTTP request looks like so: 1 GET /tutorials/other/top-20-mysql-best-practices/ HTTP/1.1 2 Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.84 Once the HTML loads, the browser will start sending GET requests for images that may look like this: 1 Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.86 2 Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.84 Web forms can be set to use the GET method. Here's an example. 1 50 2 3 53 4 55 5 GET /tutorials/other/top-20-mysql-best-practices/ HTTP/1.10 58 GET /tutorials/other/top-20-mysql-best-practices/ HTTP/1.12 GET /tutorials/other/top-20-mysql-best-practices/ HTTP/1.14 Accept-Language: en-us,en;q=0.51 When that form is submitted, the HTTP request begins like this: 1 Accept-Language: en-us,en;q=0.53 2 Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.84 You can see that each form input was added to the query string. POST: Send Data to the ServerEven though you can send data to the server using GET and the query string, in many cases POST will be preferable. Sending large amounts of data using GET is not practical and has limitations. POST requests are most commonly sent by web forms. Let's change the previous form example to a POST method. 1 Accept-Language: en-us,en;q=0.57 2 3 53 4 55 5 GET /tutorials/other/top-20-mysql-best-practices/ HTTP/1.10 58 GET /tutorials/other/top-20-mysql-best-practices/ HTTP/1.12 GET /tutorials/other/top-20-mysql-best-practices/ HTTP/1.14 Accept-Language: en-us,en;q=0.51 Submitting that form creates an HTTP request like this: 1 GET /tutorials/other/top-20-mysql-best-practices/ HTTP/1.110 2 GET /tutorials/other/top-20-mysql-best-practices/ HTTP/1.112 3 User-Agent: Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.1.5) Gecko/20091102 Firefox/3.5.5 (.NET CLR 3.5.30729) 4 Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8 5 Accept-Language: en-us,en;q=0.5 GET /tutorials/other/top-20-mysql-best-practices/ HTTP/1.10 GET /tutorials/other/top-20-mysql-best-practices/ HTTP/1.11 GET /tutorials/other/top-20-mysql-best-practices/ HTTP/1.12 GET /tutorials/other/top-20-mysql-best-practices/ HTTP/1.13 GET /tutorials/other/top-20-mysql-best-practices/ HTTP/1.14 GET /tutorials/other/top-20-mysql-best-practices/ HTTP/1.15 GET /tutorials/other/top-20-mysql-best-practices/ HTTP/1.16 GET /tutorials/other/top-20-mysql-best-practices/ HTTP/1.17 GET /tutorials/other/top-20-mysql-best-practices/ HTTP/1.18 GET /tutorials/other/top-20-mysql-best-practices/ HTTP/1.128 20 GET /tutorials/other/top-20-mysql-best-practices/ HTTP/1.130 22 GET /tutorials/other/top-20-mysql-best-practices/ HTTP/1.132 38 User-Agent: Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.1.5) Gecko/20091102 Firefox/3.5.5 (.NET CLR 3.5.30729)0 GET /tutorials/other/top-20-mysql-best-practices/ HTTP/1.135 There are three important things to note here:
POST method requests can also be made via AJAX, applications, cURL, etc. And all file upload forms are required to use the POST method. HEAD: Retrieve Header InformationHEAD is identical to GET, except the server does not return the content in the HTTP response. When you send a HEAD request, it means that you are only interested in the response code and the HTTP headers, not the document itself. With this method, the browser can check if a document has been modified, for caching purposes. It can also check if the document exists at all. For example, if you have a lot of links on your website, you can periodically send HEAD requests to all of them to check for broken links. This will work much faster than using GET. HTTP Response StructureAfter the browser sends the HTTP request, the server responds with an HTTP response. Excluding the content, it looks like this: The first piece of data is the protocol. This is again usually HTTP/1.x or HTTP/1.1 on modern servers. The next part is the status code, followed by a short message. Code 200 means that our GET request was successful and the server will return the contents of the requested document, right after the headers. We've all seen 404 pages. This number actually comes from the status code part of the HTTP response. If a GET request is made for a path that the server cannot find, it will respond with a 404 instead of 200. The rest of the response contains headers just like the HTTP request. These values can contain information about the server software, when the page/file was last modified, the MIME type, etc... Again, most of those headers are actually optional. HTTP Status Codes
200 OKAs mentioned before, this status code is sent in response to a successful request. 206 Partial ContentIf an application requests only a range of the requested file, the 206 code is returned. It's most commonly used with download managers that can stop and resume a download, or split the download into pieces. 404 Not FoundWhen the requested page or file was not found, a 404 response code is sent by the server. 401 UnauthorizedPassword-protected web pages send this code. If you don't enter a login correctly, you may see the following in your browser. Note that this only applies to HTTP password-protected pages that pop up login prompts like this: 403 ForbiddenIf you are not allowed to access a page, this code may be sent to your browser. This often happens when you try to open a URL for a folder that contains no index page. If the server settings do not allow the display of the folder contents, you will get a 403 error. For example, on my local server I created an images folder. Inside this folder I put an .htaccess file with this line: " 321". Now when I try to open http://localhost/images/, I see this: There are other ways in which access can be blocked and 403 responses can be sent. For example, you can block by IP address, with the help of some htaccess directives. 1 GET /tutorials/other/top-20-mysql-best-practices/ HTTP/1.137 2 GET /tutorials/other/top-20-mysql-best-practices/ HTTP/1.139 3 GET /tutorials/other/top-20-mysql-best-practices/ HTTP/1.141 4 GET /tutorials/other/top-20-mysql-best-practices/ HTTP/1.143 5 GET /tutorials/other/top-20-mysql-best-practices/ HTTP/1.145 302 (or 307) Moved Temporarily & 301 Moved PermanentlyThese two codes are used for redirecting a browser. For example, when you use a URL shortening service, such as bit.ly, that's exactly how they forward the people who click on their links. Both 302 and 301 are handled very similarly by the browser, but they can have different meanings to search engine spiders. For instance, if your website is down for maintenance, you may redirect to another location using 302. The search engine spider will continue checking your page later in the future. But if you redirect using 301, it will tell the spider that your website has moved to that location permanently. For example, https://code.tutsplus.com redirects to https://code.tutsplus.com—that is the new canonical URL. 500 Internal Server ErrorThis code is usually seen when a web script crashes. Most CGI scripts do not output errors directly to the browser, unlike PHP. If there are any fatal errors, they will just send a 500 status code. And the programmer then needs to search the server error logs to find the error messages. Complete ListYou can find the complete list of HTTP status codes with their explanations on Wikipedia. HTTP Headers in HTTP RequestsNow, we'll review some of the most common HTTP headers found in HTTP requests. Almost all of these headers can be found in the 311 array in PHP. You can also use the 310 function to retrieve all headers at once.
|