Cara menggunakan explain xhtml

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.

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.

Cara menggunakan explain xhtml
Cara menggunakan explain xhtml
Cara menggunakan explain xhtml

Example

When 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.1
0
GET /tutorials/other/top-20-mysql-best-practices/ HTTP/1.1
1
GET /tutorials/other/top-20-mysql-best-practices/ HTTP/1.1
2
GET /tutorials/other/top-20-mysql-best-practices/ HTTP/1.1
3
GET /tutorials/other/top-20-mysql-best-practices/ HTTP/1.1
4
GET /tutorials/other/top-20-mysql-best-practices/ HTTP/1.1
5
GET /tutorials/other/top-20-mysql-best-practices/ HTTP/1.1
6
GET /tutorials/other/top-20-mysql-best-practices/ HTTP/1.1
7
GET /tutorials/other/top-20-mysql-best-practices/ HTTP/1.1
8
GET /tutorials/other/top-20-mysql-best-practices/ HTTP/1.1
9
2
0
2
1
2
2
2
3

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
2
5
2
2
7
3
2
9
4
Host: code.tutsplus.com
1
5
Host: code.tutsplus.com
3
GET /tutorials/other/top-20-mysql-best-practices/ HTTP/1.1
0
Host: code.tutsplus.com
5
GET /tutorials/other/top-20-mysql-best-practices/ HTTP/1.1
2
Host: code.tutsplus.com
7
GET /tutorials/other/top-20-mysql-best-practices/ HTTP/1.1
4
Host: code.tutsplus.com
9
GET /tutorials/other/top-20-mysql-best-practices/ HTTP/1.1
6
3
1
GET /tutorials/other/top-20-mysql-best-practices/ HTTP/1.1
8
3
3
2
0
3
5
2
2
3
7
3
8
3
9
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
4
0
4
1
4
2
4
3
4
4
4
5
4
6

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 Headers

I 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:

  • 3
    10 gets the request headers. You can also use the 
    3
    11 array.
  • 3
    12 gets the response headers.

Further in the article, we will see some code examples in PHP.

HTTP Request Structure

Cara menggunakan explain xhtml
Cara menggunakan explain xhtml
Cara menggunakan explain xhtml

The first line of the HTTP request is called the request line and consists of three parts:

  • The "method" indicates what kind of request this is. The most common methods are GET, POST, and HEAD.
  • The "path" is generally the part of the URL that comes after the host (domain). For example, when requesting "https://code.tutsplus.com/tutorials/top-20-mysql-best-practices--net-7855" , the path portion is "/tutorials/top-20-mysql-best-practices--net-7855".
  • The 
    3
    13 part contains 
    3
    14 and the version, which is usually 1.1 in modern browsers.

The remainder of the request contains HTTP headers as 

3
15 pairs on each line. These contain various information about the HTTP request and your browser. For example, the 
3
16 line provides information on the browser version and the Operating System you are using. 
3
17 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 Methods

The 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 Document

This 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.8
4

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.8
6
2
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
4

Web forms can be set to use the GET method. Here's an example.

1
5
0
2
3
5
3
4
5
5
5
GET /tutorials/other/top-20-mysql-best-practices/ HTTP/1.1
0
5
8
GET /tutorials/other/top-20-mysql-best-practices/ HTTP/1.1
2
GET /tutorials/other/top-20-mysql-best-practices/ HTTP/1.1
4
Accept-Language: en-us,en;q=0.5
1

When that form is submitted, the HTTP request begins like this:

1
Accept-Language: en-us,en;q=0.5
3
2
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
4

You can see that each form input was added to the query string.

POST: Send Data to the Server

Even 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.5
7
2
3
5
3
4
5
5
5
GET /tutorials/other/top-20-mysql-best-practices/ HTTP/1.1
0
5
8
GET /tutorials/other/top-20-mysql-best-practices/ HTTP/1.1
2
GET /tutorials/other/top-20-mysql-best-practices/ HTTP/1.1
4
Accept-Language: en-us,en;q=0.5
1

Submitting that form creates an HTTP request like this:

1
GET /tutorials/other/top-20-mysql-best-practices/ HTTP/1.1
10
2
GET /tutorials/other/top-20-mysql-best-practices/ HTTP/1.1
12
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.1
0
GET /tutorials/other/top-20-mysql-best-practices/ HTTP/1.1
1
GET /tutorials/other/top-20-mysql-best-practices/ HTTP/1.1
2
GET /tutorials/other/top-20-mysql-best-practices/ HTTP/1.1
3
GET /tutorials/other/top-20-mysql-best-practices/ HTTP/1.1
4
GET /tutorials/other/top-20-mysql-best-practices/ HTTP/1.1
5
GET /tutorials/other/top-20-mysql-best-practices/ HTTP/1.1
6
GET /tutorials/other/top-20-mysql-best-practices/ HTTP/1.1
7
GET /tutorials/other/top-20-mysql-best-practices/ HTTP/1.1
8
GET /tutorials/other/top-20-mysql-best-practices/ HTTP/1.1
28
2
0
GET /tutorials/other/top-20-mysql-best-practices/ HTTP/1.1
30
2
2
GET /tutorials/other/top-20-mysql-best-practices/ HTTP/1.1
32
3
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)
0
GET /tutorials/other/top-20-mysql-best-practices/ HTTP/1.1
35

There are three important things to note here:

  • The path in the first line is simply 
    3
    18, and there is no query string anymore.
  • 3
    19 and 
    3
    20 headers have been added, which provide information about the data being sent.
  • All the data is now sent after the headers, with the same format as the query string.

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 Information

HEAD 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 Structure

After the browser sends the HTTP request, the server responds with an HTTP response. Excluding the content, it looks like this:

Cara menggunakan explain xhtml
Cara menggunakan explain xhtml
Cara menggunakan explain xhtml

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

  • 200s are used for successful requests.
  • 300s are for redirections.
  • 400s are used if there was a problem with the request.
  • 500s are used if there was a problem with the server.

200 OK

As mentioned before, this status code is sent in response to a successful request.

206 Partial Content

If 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 Found

Cara menggunakan explain xhtml
Cara menggunakan explain xhtml
Cara menggunakan explain xhtml

When the requested page or file was not found, a 404 response code is sent by the server.

401 Unauthorized

Password-protected web pages send this code. If you don't enter a login correctly, you may see the following in your browser.

Cara menggunakan explain xhtml
Cara menggunakan explain xhtml
Cara menggunakan explain xhtml

Note that this only applies to HTTP password-protected pages that pop up login prompts like this:

Cara menggunakan explain xhtml
Cara menggunakan explain xhtml
Cara menggunakan explain xhtml

403 Forbidden

If 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: "

3
21". Now when I try to open http://localhost/images/, I see this:

Cara menggunakan explain xhtml
Cara menggunakan explain xhtml
Cara menggunakan explain xhtml

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.1
37
2
GET /tutorials/other/top-20-mysql-best-practices/ HTTP/1.1
39
3
GET /tutorials/other/top-20-mysql-best-practices/ HTTP/1.1
41
4
GET /tutorials/other/top-20-mysql-best-practices/ HTTP/1.1
43
5
GET /tutorials/other/top-20-mysql-best-practices/ HTTP/1.1
45

302 (or 307) Moved Temporarily & 301 Moved Permanently

These 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 Error

Cara menggunakan explain xhtml
Cara menggunakan explain xhtml
Cara menggunakan explain xhtml

This 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 List

You can find the complete list of HTTP status codes with their explanations on Wikipedia.

HTTP Headers in HTTP Requests

Now, we'll review some of the most common HTTP headers found in HTTP requests.

Almost all of these headers can be found in the 

3
11 array in PHP. You can also use the 
3
10 function to retrieve all headers at once.

3
24

An HTTP request is sent to a specific IP address. But since most servers are capable of hosting multiple websites under the same IP, they must know which domain name the browser is looking for.

1
Host: code.tutsplus.com

This is basically the host name, including the domain and the subdomain.

In PHP, it can be found as 

3
25 or 
3
26.

3
16

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)

This header can carry several pieces of information, such as:

  • browser name and version
  • operating system name and version
  • default language

This is how websites can collect certain general information about their surfers' systems. For example, they can detect if the surfer is using a cellphone browser and redirect them to a mobile version of their website which works better on smaller screens.

In PHP, it can be found with: 

3
28.

1
GET /tutorials/other/top-20-mysql-best-practices/ HTTP/1.1
51
2
GET /tutorials/other/top-20-mysql-best-practices/ HTTP/1.1
53
3
GET /tutorials/other/top-20-mysql-best-practices/ HTTP/1.1
55

3
29

1
Accept-Language: en-us,en;q=0.5

This header displays the default language setting of the user. If a website has different language versions, it can redirect a new surfer based on this data.

It can carry multiple languages, separated by commas. The first one is the preferred language, and each other listed language can carry a "

3
30" value, which is an estimate of the user's preference for the language (min. 0 max. 1).

In PHP, it can be found as: 

3
31.

1
GET /tutorials/other/top-20-mysql-best-practices/ HTTP/1.1
59
2
GET /tutorials/other/top-20-mysql-best-practices/ HTTP/1.1
61
3
GET /tutorials/other/top-20-mysql-best-practices/ HTTP/1.1
55

3
17

1
GET /tutorials/other/top-20-mysql-best-practices/ HTTP/1.1
1

Most modern browsers support gzip and will send this in the header. The web server then can send the HTML output in a compressed format. This can reduce the size by up to 80% to save bandwidth and time.

In PHP, it can be found as: 

3
33. However, when you use the 
3
34 callback function, it will check this value automatically, so you don't need to.

1
GET /tutorials/other/top-20-mysql-best-practices/ HTTP/1.1
67
2
GET /tutorials/other/top-20-mysql-best-practices/ HTTP/1.1
69
3
GET /tutorials/other/top-20-mysql-best-practices/ HTTP/1.1
71

3
35

If a web document is already cached in your browser, and you visit it again, your browser can check if the document has been updated by sending this:

1
GET /tutorials/other/top-20-mysql-best-practices/ HTTP/1.1
73

If it was not modified since that date, the server will send a "304 Not Modified" response code, and no content—and the browser will load the content from the cache.

In PHP, it can be found as: 

3
36.

1
GET /tutorials/other/top-20-mysql-best-practices/ HTTP/1.1
75
2
3
GET /tutorials/other/top-20-mysql-best-practices/ HTTP/1.1
78
4
GET /tutorials/other/top-20-mysql-best-practices/ HTTP/1.1
80
5
GET /tutorials/other/top-20-mysql-best-practices/ HTTP/1.1
0
GET /tutorials/other/top-20-mysql-best-practices/ HTTP/1.1
83
GET /tutorials/other/top-20-mysql-best-practices/ HTTP/1.1
2
GET /tutorials/other/top-20-mysql-best-practices/ HTTP/1.1
85
GET /tutorials/other/top-20-mysql-best-practices/ HTTP/1.1
4
GET /tutorials/other/top-20-mysql-best-practices/ HTTP/1.1
6
GET /tutorials/other/top-20-mysql-best-practices/ HTTP/1.1
88
GET /tutorials/other/top-20-mysql-best-practices/ HTTP/1.1
8
GET /tutorials/other/top-20-mysql-best-practices/ HTTP/1.1
90
2
0
GET /tutorials/other/top-20-mysql-best-practices/ HTTP/1.1
92
2
2
GET /tutorials/other/top-20-mysql-best-practices/ HTTP/1.1
94
3
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)
0
GET /tutorials/other/top-20-mysql-best-practices/ HTTP/1.1
55

There is also an HTTP header named Etag, which can be used to make sure the cache is current. We'll talk about this shortly.

3
37

As the name suggests, this sends the cookies stored in your browser for that domain.

1
GET /tutorials/other/top-20-mysql-best-practices/ HTTP/1.1
99

These are name=value pairs separated by semicolons. Cookies can also contain the session id.

In PHP, individual cookies can be accessed with the 

3
38 array. You can directly access the session variables using the 
3
39 array, and if you need the session id, you can use the 
3
40 function instead of the cookie.

1
2
01
2
2
03
3
2
05
4
2
07
5
2
09
GET /tutorials/other/top-20-mysql-best-practices/ HTTP/1.1
0
2
11
GET /tutorials/other/top-20-mysql-best-practices/ HTTP/1.1
2
2
13

3
41

As the name suggests, this HTTP header contains the referring URL.

For example, if I visit the Envato Tuts+ Code homepage and click on an article link, this header is sent to my browser:

1
2
15

In PHP, it can be found as 

3
42.

1
2
17
2
3
2
20
4
5
2
23
GET /tutorials/other/top-20-mysql-best-practices/ HTTP/1.1
0
2
25
GET /tutorials/other/top-20-mysql-best-practices/ HTTP/1.1
2
GET /tutorials/other/top-20-mysql-best-practices/ HTTP/1.1
4
2
28
GET /tutorials/other/top-20-mysql-best-practices/ HTTP/1.1
6
GET /tutorials/other/top-20-mysql-best-practices/ HTTP/1.1
8
2
31
2
0
2
2
GET /tutorials/other/top-20-mysql-best-practices/ HTTP/1.1
94
3
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)
0
GET /tutorials/other/top-20-mysql-best-practices/ HTTP/1.1
55
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
2
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)
4
2
41
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
2
43
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
2
45

You may have noticed the word "referrer" is misspelled as "referer". Unfortunately it made into the official HTTP specifications like that and got stuck.

Authorization

When a web page asks for authorization, the browser opens a login window. When you enter a username and password in this window, the browser sends another HTTP request, but this time it contains this header.

1
2
47

The data inside the header is base64 encoded. For example, 

3
43 would return 
3
44.

In PHP, these values can be found as 

3
45 and 
3
46.

More on this when we talk about the WWW-Authenticate header.

HTTP Headers in HTTP Responses

Now we are going to look at some of the most common HTTP headers found in HTTP responses.

In PHP, you can set response headers using the 

3
47 function. PHP already sends certain headers automatically, for loading the content, setting cookies, etc. You can see the headers that are sent, or will be sent, with the
3
12 function. You can check if the headers have been sent already with the 
3
49 function.

3
50

Here's the definition from w3.org:

The Cache-Control general-header field is used to specify directives which MUST be obeyed by all caching mechanisms along the request/response chain.

These "caching mechanisms" include gateways and proxies that your ISP may be using.

For example:

1
3
3

3
51 means that the response may be cached by anyone. 
3
52 indicates how many seconds the cache is valid for. Allowing your website to be cached can reduce server load and bandwidth, as well as improving load times in the browser.

Caching can also be prevented by using the 

3
53 directive.

1
2
3

For more detailed info, see .

3
19

This header indicates the "MIME type" of the document. The browser then decides how to interpret the contents based on this. For example, an HTML page (or a PHP script with HTML output) may return this:

1
3
5

3
55 is the type, and 
3
56 is the subtype of the document. The header can also contain more information, such as charset.

For a GIF image, this may be sent:

1
2
55

The browser can decide to use an external application or browser extension based on the MIME type. For example, this will cause Adobe Reader or the browser's built-in PDF reader to be loaded:

1
2
57

When loading directly, Apache can usually detect the MIME type of a document and send the appropriate header. Also, most browsers have some amount of fault tolerance and auto-detection of the MIME types, in case the headers are wrong or not present.

You can find a list of common MIME types in the MDN Web Docs.

In PHP, you can use the 

3
57 function to detect the MIME type of a file.

3
58

This header instructs the browser to open a file download box, instead of trying to parse the content. For example:

1
2
59

That will cause the browser to do this:

Cara menggunakan explain xhtml
Cara menggunakan explain xhtml
Cara menggunakan explain xhtml

Note that the appropriate 

3
19 header should also be sent along with this:

1
2
61
2
2
59

3
20

When content is going to be transmitted to the browser, the server can indicate its size (in bytes) using this header.

1
2
65

This is especially useful for file downloads. That's how the browser can determine the progress of the download.

For example, here is a dummy script I wrote, which simulates a large download.

1
2
67
2
2
69
3
2
71
4
2
73
5
2
75
GET /tutorials/other/top-20-mysql-best-practices/ HTTP/1.1
0
2
77
GET /tutorials/other/top-20-mysql-best-practices/ HTTP/1.1
2
GET /tutorials/other/top-20-mysql-best-practices/ HTTP/1.1
4
2
80
GET /tutorials/other/top-20-mysql-best-practices/ HTTP/1.1
6
2
82
GET /tutorials/other/top-20-mysql-best-practices/ HTTP/1.1
8
2
84
2
0
2
2
2
87
3
8
2
89
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.1
55

The result is:

Cara menggunakan explain xhtml
Cara menggunakan explain xhtml
Cara menggunakan explain xhtml

Now I am going to comment out the Content-Length header:

1
2
67
2
2
69
3
2
97
4
2
99
5
2
75
GET /tutorials/other/top-20-mysql-best-practices/ HTTP/1.1
0
2
77
GET /tutorials/other/top-20-mysql-best-practices/ HTTP/1.1
2
GET /tutorials/other/top-20-mysql-best-practices/ HTTP/1.1
4
2
80
GET /tutorials/other/top-20-mysql-best-practices/ HTTP/1.1
6
2
82
GET /tutorials/other/top-20-mysql-best-practices/ HTTP/1.1
8
2
84
2
0
2
2
2
87
3
8
2
89
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.1
55

Now the result is:

Cara menggunakan explain xhtml
Cara menggunakan explain xhtml
Cara menggunakan explain xhtml

The browser can only tell you how many bytes have been downloaded, but it does not know the total amount. And the progress bar is not showing the progress.

3
61

This is another header that is used for caching purposes. It looks like this:

1
3
1

The web server may send this header with every document it serves. The value can be based on the last modify date, the file size, or even the checksum value of a file. The browser then saves this value as it caches the document. The next time the browser requests the same file, it sends this in the HTTP request:

1
Host: code.tutsplus.com
21

If the Etag value of the document matches that, the server will send a 304 code instead of 200, and no content. The browser will load the contents from its cache.

3
62

As the name suggests, this header indicates the last modify date of the document, in GMT format:

1
3
7

1
Host: code.tutsplus.com
25
2
3
Host: code.tutsplus.com
28

It offers another way for the browser to cache a document. The browser may send this in the HTTP request:

1
GET /tutorials/other/top-20-mysql-best-practices/ HTTP/1.1
73

We already talked about this earlier, in the

3
35 section.

3
64

This header is used for redirections. If the response code is 301 or 302, the server must also send this header. For example, when you go to https://code.tutsplus.com, your browser will receive this:

1
Host: code.tutsplus.com
32
2
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
4
3
Host: code.tutsplus.com
36
4
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
4

In PHP, you can redirect a surfer like so:

1
Host: code.tutsplus.com
40

By default, that will send a 302 response code. If you want to send a 301 instead:

1
Host: code.tutsplus.com
42

3
65

When a website wants to set or update a cookie in your browser, it will use this header.

1
Host: code.tutsplus.com
44
2
Host: code.tutsplus.com
46

Each cookie is sent as a separate header. Note that cookies set via JavaScript do not go through HTTP headers.

In PHP, you can set cookies using the 

3
66 function, and PHP sends the appropriate HTTP headers.

1
Host: code.tutsplus.com
48

Which causes this header to be sent:

1
Host: code.tutsplus.com
50

If the expiration date is not specified, the cookie is deleted when the browser window is closed.

3
67

A website may send this header to authenticate a user through HTTP. When the browser sees this header, it will open up a login dialogue window.

1
Host: code.tutsplus.com
52

Which looks like this:

Cara menggunakan explain xhtml
Cara menggunakan explain xhtml
Cara menggunakan explain xhtml

There is a section in the PHP manual that has code samples on how to do this in PHP.

1
Host: code.tutsplus.com
54
2
Host: code.tutsplus.com
56
3
Host: code.tutsplus.com
58
4
Host: code.tutsplus.com
60
5
Host: code.tutsplus.com
62
GET /tutorials/other/top-20-mysql-best-practices/ HTTP/1.1
0
Host: code.tutsplus.com
64
GET /tutorials/other/top-20-mysql-best-practices/ HTTP/1.1
2
Host: code.tutsplus.com
66
GET /tutorials/other/top-20-mysql-best-practices/ HTTP/1.1
4
Host: code.tutsplus.com
68
GET /tutorials/other/top-20-mysql-best-practices/ HTTP/1.1
6
GET /tutorials/other/top-20-mysql-best-practices/ HTTP/1.1
55

3
68

This header is usually set when the returned content is compressed.

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)
1

In PHP, if you use the 

3
34 callback function, it will be set automatically for you.

How to Send HTTP Headers

After reading the tutorial up to this point, you should have a good idea of what HTTP headers are and what their different values mean. Some headers are sent and received automatically when you make a request to a server and get a response back.

However, there will be situations where you want to send your own custom headers besides the ones sent by the client or server.

One of the most common ways of sending your own headers in a request is by using the cURL library in PHP. The library comes with a bunch of functions to handle all your needs. There are four basic steps involved:

  1. You use 
    3
    70 to start your cURL session. You can pass it the URL you want to request.
  2. The 
    3
    71 function is used to configure the request according to your needs. This is where you can set your own headers by using the 
    3
    72 option.
  3. After you have set all the options, you can execute the request by calling 
    3
    73.
  4. Finally, you can close the session by calling the 
    3
    74 function.

Here is a basic example that sends a request to https://code.tutsplus.com/tutorials.

1
Host: code.tutsplus.com
74
2
3
Host: code.tutsplus.com
77
4
5
Host: code.tutsplus.com
80
GET /tutorials/other/top-20-mysql-best-practices/ HTTP/1.1
0
Host: code.tutsplus.com
82
GET /tutorials/other/top-20-mysql-best-practices/ HTTP/1.1
2
Host: code.tutsplus.com
84
GET /tutorials/other/top-20-mysql-best-practices/ HTTP/1.1
4
Host: code.tutsplus.com
86
GET /tutorials/other/top-20-mysql-best-practices/ HTTP/1.1
6
GET /tutorials/other/top-20-mysql-best-practices/ HTTP/1.1
8
Host: code.tutsplus.com
89
2
0
Host: code.tutsplus.com
91
2
2
3
8
Host: code.tutsplus.com
94
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)
2
Host: code.tutsplus.com
97
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
Host: code.tutsplus.com
99

You can learn more about cURL by reading these two tutorials. They cover all the basics of the library to help you get started.

  • Cara menggunakan explain xhtml

    Techniques for Mastering cURL

    Cara menggunakan explain xhtml

    Burak Guzel

    08 Jan 2010

  • Cara menggunakan explain xhtml
    Cara menggunakan explain xhtml
    Cara menggunakan explain xhtml

    How to Use cURL in PHP

    Cara menggunakan explain xhtml
    Cara menggunakan explain xhtml
    Cara menggunakan explain xhtml

    Sajal Soni

    25 Mar 2021

If you want to send response headers in PHP, then you should use the 

3
75 function. Among other things, one common use is redirecting visitors to other pages. This can be done by using the
3
64 header. Here is an example:

1
Host: code.tutsplus.com
74
2
3
3
04
4
5
3
07
GET /tutorials/other/top-20-mysql-best-practices/ HTTP/1.1
0
Host: code.tutsplus.com
99

You have to remember to call the 

3
75 function before any kind of output either in HTML or in PHP. Even blank output is not permitted. Otherwise, you will get the Headers already sent error.

Conclusion

Thanks for reading. I hope this article was a good starting point for learning about HTTP headers. If you want to take your web development further, check out some of the popular files on CodeCanyon. These scripts, apps, templates, and plugins can save you precious development time and help you add new features quickly and easily. 

The Best PHP Scripts on CodeCanyon

Explore thousands of the best and most useful PHP scripts ever created on CodeCanyon.

Cara menggunakan explain xhtml
Cara menggunakan explain xhtml
Cara menggunakan explain xhtml

Here are a few of the best-selling and up-and-coming PHP scripts available on CodeCanyon for 2021.

  • Cara menggunakan explain xhtml
    Cara menggunakan explain xhtml
    Cara menggunakan explain xhtml

    19 Best PHP Event Calendar and Booking Scripts... and 3 Free Options

    Cara menggunakan explain xhtml
    Cara menggunakan explain xhtml
    Cara menggunakan explain xhtml

    Monty Shokeen

    19 Jul 2021

  • Cara menggunakan explain xhtml
    Cara menggunakan explain xhtml
    Cara menggunakan explain xhtml

    11 Best PHP URL Shortener Scripts (Free and Premium)

    Cara menggunakan explain xhtml
    Cara menggunakan explain xhtml
    Cara menggunakan explain xhtml

    Monty Shokeen

    20 Jun 2022

  • Cara menggunakan explain xhtml
    Cara menggunakan explain xhtml
    Cara menggunakan explain xhtml

    18 Best Contact Form PHP Scripts for 2022

    Cara menggunakan explain xhtml
    Cara menggunakan explain xhtml
    Cara menggunakan explain xhtml

    Monty Shokeen

    11 May 2022

  • Cara menggunakan explain xhtml
    Cara menggunakan explain xhtml
    Cara menggunakan explain xhtml

    Comparing the 5 Best PHP Form Builders (And 3 Free Scripts)

    Cara menggunakan explain xhtml
    Cara menggunakan explain xhtml
    Cara menggunakan explain xhtml

    Monty Shokeen

    28 Nov 2021

  • Cara menggunakan explain xhtml
    Cara menggunakan explain xhtml
    Cara menggunakan explain xhtml

    Create Beautiful Forms With PHP Form Builder

    Cara menggunakan explain xhtml
    Cara menggunakan explain xhtml
    Cara menggunakan explain xhtml

    Ashraff Hathibelagal

    20 May 2019

This post has been updated with contributions from Monty Shokeen. Monty is a full-stack developer who also loves to write tutorials, and to learn about new JavaScript libraries.