Understand all aspects of HTTPHeaders-HTML/Xhtml-webpage Creation

Source: Internet
Author: User
Tags server array server error log usleep w3 total cache
Both frontend and backend services will deal with HTTPHeaders from time to time, and understanding it will undoubtedly be of great help to Web development. This article will allow me to learn about httpheaders. What is HTTP Headers?

HTTP is written by "Hypertext Transfer Protocol". This Protocol is used throughout the world wide web. Most of the content you see in your browser is transmitted over http. For example, this article.

HTTP Headers is the core of HTTP requests. It carries information about the client browser, request page, server, and so on.


Example

When you type a url in the address bar of your browser, your browser will be similar to the following http request:
GET /tutorials/other/top-20-mysql-best-practices/ HTTP/1.1
Host: net.tutsplus.com
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)
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: en-us,en;q=0.5
Accept-Encoding: gzip,deflate
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7
Keep-Alive: 300
Connection: keep-alive
Cookie: PHPSESSID=r2t5uvjq435r4q7ib3vtdjq120
Pragma: no-cache
Cache-Control: no-cache

The first Line is called "Request Line". It describes the basic information of the Request, and the rest is the HTTP headers.

After the request is complete, your browser may receive the following HTTP response:

HTTP/1.x 200 OK
Transfer-Encoding: chunked
Date: Sat, 28 Nov 2009 04:36:25 GMT
Server: LiteSpeed
Connection: close
X-Powered-By: W3 Total Cache/0.8
Pragma: public
Expires: Sat, 28 Nov 2009 05:36:25 GMT
Etag: "pub1259380237;gz"
Cache-Control: max-age=3600, public
Content-Type: text/html; charset=UTF-8
Last-Modified: Sat, 28 Nov 2009 03:50:37 GMT
X-Pingback: http://net.tutsplus.com/xmlrpc.php
Content-Encoding: gzip
Vary: Accept-Encoding, Cookie, User-Agent

The first Line is called "Status Line", which is followed by http headers. When the empty Line is complete, the output starts (in this case, some html output is used ).

However, HTTP headers cannot be seen when you view the page source code, although they are transmitted to the browser together with what you can see.

This HTTP request also sends receiving requests for some other resources, such as examples, css files, and js files.

Let's take a look at the details.

How to view HTTP Headers

The following FireFox extensions can help you analyze HTTP headers:

1. firebug


2. Live HTTP Headers


3. In PHP:

  • Getallheaders () is used to obtain the request header. You can also use the $ _ SERVER array.
  • Headers_list () is used to obtain the response header.

The following are some examples of using php.

HTTP Request Structure


The first line, known as "first line", contains three parts:

  • "Method" indicates the type of request. The most common request types include GET, POST, and HEAD.
  • "Path" represents the path after the host. For example, when you request "pipeline, path will be"/tutorials/other/top-20-mysql-best-practices /".
  • "Protocol" contains "HTTP" and version numbers. modern browsers use 1.1.

The remaining part of each line is a "Name: Value" pair. They contain a variety of information about requests and your browser. For example, "User-Agent" indicates your browser version and the operating system you use ." Accept-Encoding "will tell the server that your browser can Accept compressed output similar to gzip.

Most of these headers are optional. HTTP requests can even be simplified as follows:

GET /tutorials/other/top-20-mysql-best-practices/ HTTP/1.1
Host: net.tutsplus.com

And you can still receive valid responses from the server.

Request type

The three most common request types are GET, POST, and HEAD. You may be familiar with the first two types of requests during html writing.

GET: GET a document

Most of the html, images, js, css ,... All requests are sent through the GET method. It is the main method to 

For example, to get the Nettuts + Article, the first line of http request usually looks like this:

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

Once html loading is complete, the browser will send a GET request to GET the image, as shown below:

GET /wp-content/themes/tuts_theme/images/header_bg_tall.png HTTP/1.1

The form can also be sent using the GET method. The following is an example:


When the form is submitted, the HTTP request will look like this:

GET /foo.php?first_name=John&last_name=Doe&action=Submit HTTP/1.1
...

You can append the form input to the query string to the server.

POST: send data to the server

Although you can use the GET method to append data to a url and send it to the server, it is more appropriate to use POST to send data to the server in many cases. It is unrealistic to send a large amount of data through GET, which has certain limitations.

It is common practice to use POST requests to send form data. Let's take the above example and convert it to the POST method:


The following HTTP request is created when you submit this form:

POST /foo.php HTTP/1.1
Host: localhost
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)
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: en-us,en;q=0.5
Accept-Encoding: gzip,deflate
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7
Keep-Alive: 300
Connection: keep-alive
Referer: http://localhost/test.php
Content-Type: application/x-www-form-urlencoded
Content-Length: 43
first_name=John&last_name=Doe&action=Submit

There are three points to note:

  • The path of the first line has become simple/foo. php, and no query string is available.
  • Added the Content-Type and Content-Lenght headers, which provide information about the sent messages.
  • All data is sent as query strings after headers.

POST requests can also be used in AJAX, applications, cURL... . In addition, all file upload forms are required to use the POST method.

HEAD: receive header information

The HEAD is similar to GET, except that the HEAD does not accept the content of the HTTP response. When you send a HEAD request, it means you are only interested in the HTTP header, not the document itself.

This method allows the browser to determine whether the page has been modified to control the cache. You can also determine whether the requested document exists.

For example, if your website has many links, you can simply send HEAD requests to them to determine whether a dead chain exists. This is much faster than GET.

Http Response Structure

After the browser sends an HTTP request, the server will respond to the request through an HTTP response. If you don't care about the content, the request looks like this:


The first valuable information is the protocol. Currently, all servers use HTTP/1.x or HTTP/1.1.

The following brief information indicates the status. Code 200 means that our request has been successfully sent, and the server will return the requested document after the header information.

We have all seen the 404 page. When I request a non-existent path from the server, the server uses 404 instead of 200 to respond to us.

The remaining response content is similar to the HTTP request. This content is about the server software, when the page/file has been modified, mime type, etc...

Likewise, this header information is optional.

HTTP status code
  • 200 indicates that the request is successful.
  • 300 to indicate redirection.
  • 400 indicates a request error.
  • 500 indicates a server error.

200 success (OK)

As mentioned above, 200 indicates that the request is successful.

Part 1 (Partial Content)

If an application only requests files within a certain range, 206 is returned.

This is usually used for download management, resumable upload or multipart download.

404 Not Found)


Easy to understand

401 Unauthorized)

The password-protected page returns this status. If you do not enter the correct password, you will see the following information in the browser:


Note that this is only a password-protected page. The pop-up box for requesting a password is as follows:


403 Forbidden)

If you do not have the permission to access a page, 403 is returned. This usually happens when you try to open a folder without index pages. If the server does not allow you to view the directory content, you will see the 403 error.

Some other methods also send permission restrictions. For example, you can block them by using IP addresses. This requires some help from htaccess.

order allow,deny
deny from 192.168.44.201
deny from 224.39.163.12
deny from 172.16.7.92
allow from all

302 (or 307) Temporary Movement (Moved Temporarily) and 301 permanent movement (Moved Permanently)

These two statuses will appear when the browser is redirected. For example, you use a URL shortening service similar to bit. ly. This is how they know who clicked on their link.

302 and 301 are very similar for browsers, but there are some differences between search engine crawlers. For example, if your website is being maintained, you will redirect the client browser to another address with 302. Search engine crawlers will re-index your pages in the future. However, if you use 301 redirection, this means that you have told the search engine crawler that your website has been permanently moved to a new address.

500 Server Error)


This Code usually appears when the page script crashes. Most CGI scripts do not output error messages to browsers like PHP. If a fatal error occurs, they only send a 500 status code. In this case, you need to view the server error log to troubleshoot the error.

Complete list

You can find the complete HTTP status code description here.

HTTP requests in HTTP Headers

Now let's look at some common HTTP request information in HTTP headers.

All the header information can be found in the $ _ SERVER array of PHP. You can also use the getallheaders () function to obtain all header information at a time.

Host

An HTTP request is sent to a specific IP address, but most servers have the ability to host multiple websites under the same IP address. Therefore, the server must know the resources under the domain name requested by the browser.

Host: rlog.cn

This is only the basic host name, including the domain name and sub-domain name.

In PHP, you can view it through $ _ SERVER ['HTTP _ host'] or $ _ SERVER ['server _ name.

User-Agent

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 the following information:

  • The browser name and version number.
  • The operating system name and version number.
  • Default language.

This is a general method for websites to collect visitor information. For example, you can determine whether a visitor is using a mobile phone to access your website and then decide whether to direct the visitor to a mobile website that performs well at a low resolution.

In PHP, you can get the User-Agent through $ _ SERVER ['HTTP _ USER_AGENT '].

if ( strstr($_SERVER['HTTP_USER_AGENT'],'MSIE 6') ) {
echo "Please stop using IE6!";
}

Accept-Language

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

This information indicates the user's default language settings. If the website has different language versions, you can use this information to redirect your browser.

It can be separated by commas to carry multiple languages. The first language will be the preferred language. Other languages will carry a "q" value to indicate the user's preference for the language (0 ~ 1 ).

Use $ _ SERVER ["HTTP_ACCEPT_LANGUAGE"] in PHP to obtain this information.

if (substr($_SERVER['HTTP_ACCEPT_LANGUAGE'], 0, 2) == 'fr') {
header('Location: http://french.mydomain.com');
}

Accept-Encoding

Accept-Encoding: gzip,deflate

Most modern browsers support gzip compression and report this information to the server. Then the server sends the compressed HTML to the browser. This reduces the file size by nearly 80% to save download time and bandwidth.

You can use $ _ SERVER ["HTTP_ACCEPT_ENCODING"] in PHP to obtain this information. The value is automatically detected when the ob_gzhandler () method is called, so you do not need to manually detect the value.

// enables output buffering
// and all output is compressed if the browser supports it
ob_start('ob_gzhandler');

If-Modified-Since

If a page has been cached in your browser, the browser will check whether the document has been modified next time, and then it will send a header like this:

If-Modified-Since: Sat, 28 Nov 2009 06:38:19 GMT

If it has Not been Modified since this time, the server will return "304 Not Modified" and no content will be returned. The browser will automatically read content from the cache

In PHP, you can use $ _ SERVER ['HTTP _ IF_MODIFIED_SINCE '] for detection.

// assume $last_modify_time was the last the output was updated
// did the browser send If-Modified-Since header?
if(isset($_SERVER['HTTP_IF_MODIFIED_SINCE'])) {
// if the browser cache matches the modify time
if ($last_modify_time == strtotime($_SERVER['HTTP_IF_MODIFIED_SINCE'])) {
// send a 304 header, and no content
header("HTTP/1.1 304 Not Modified");
exit;
}
}

An HTTP header called Etag is used to determine whether the cached information is correct. We will explain it later.

Cookie

As the name suggests, it will send the Cookie information stored in your browser to the server.

Cookie: PHPSESSID=r2t5uvjq435r4q7ib3vtdjq120; foo=bar

It is a group of name-value pairs separated by semicolons. The Cookie can also contain the session id.

In PHP, a single Cookie can be obtained by accessing the $ _ COOKIE array. You can directly use $ _ SESSION array to obtain the session variable. If you need a session id, you can use the session_id () function to replace the cookie.

echo $_COOKIE['foo'];
// output: bar
echo $_COOKIE['PHPSESSID'];
// output: r2t5uvjq435r4q7ib3vtdjq120
session_start();
echo session_id();
// output: r2t5uvjq435r4q7ib3vtdjq120

Referer

As the name suggests, the header will contain referring url Information.

For example, if I access the Nettuts + homepage and click a link, the header information will be sent to the browser:
Referer: http://net.tutsplus.com/

In PHP, you can get this value through $ _ SERVER ['HTTP _ referer.

if (isset($_SERVER['HTTP_REFERER'])) {
$url_info = parse_url($_SERVER['HTTP_REFERER']);
// is the surfer coming from Google?
if ($url_info['host'] == 'www.google.com') {
parse_str($url_info['query'], $vars);
echo "You searched on Google for this keyword: ". $vars['q'];
}
}
// if the referring url was:
// http://www.google.com/search?source=ig&hl=en&rlz=&=&q=http+headers&aq=f&oq=&aqi=g-p1g9
// the output will be:
// You searched on Google for this keyword: http headers

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 page requires authorization, the browser will pop up a login window. After you enter the correct account, the browser will send an HTTP request, but it will include a header like this:

Authorization: Basic bXl1c2VyOm15cGFzcw==

The information contained in the header is base64 encoded. For example, base64_decode ('bxl1c2vyom15cgfzcw = ') is converted to 'myuser: mypass '.

In PHP, this value can be obtained using $ _ SERVER ['php _ AUTH_USER '] and $ _ SERVER ['php _ AUTH_PW.

For more details, refer to the WWW-Authenticate section.

HTTP Response in HTTP Headers

Now let me know some common HTTP Response Information in HTTP Headers.

In PHP, you can set the header response information through header. PHP has automatically sent some necessary header information, such as loaded content, setting cookies, etc... You can use the headers_list () function to view the sent and to-be-sent headers. You can also use the headers_sent () function to check whether the header information has been sent.

Cache-Control

The definition of w3.org is: "the Cache-Control general-header field is used to specify directives which MUST be obeyed by all caching mechanisms along The request/response chain. "among them," caching mechanisms "contains some gateway and proxy information that your ISP may use.

For example:

Cache-Control: max-age=3600, public

"Public" means that the response can be cached by anyone. "max-age" indicates the number of seconds that the cache is valid. This allows your website to be cached to reduce the download time and bandwidth, and increases the loading speed of your browser.

You can also disable caching by setting the "no-cache" command:

Cache-Control: no-cache

For more details, see w3.org.

Content-Type

This header contains the "mime-type" of the document ". The browser determines how to parse the document based on this parameter. For example, an html page (or a php page with html output) will return something like this:

Content-Type: text/html; charset=UTF-8

'Text' indicates the document type, while 'html 'indicates the document subtype. This header also contains more information, such as charset.

If it is an image, a response like this will be sent:

Content-Type: image/gif

The browser can use mime-type to determine whether to open the document using external programs or its own extensions. In the following example, Adobe Reader is called:

Content-Type: application/pdf

Load directly. Apache usually automatically determines the mime-type of the document and adds the appropriate information to the header. Most browsers have a certain degree of fault tolerance. If the header does not provide or the information is incorrectly provided, it will automatically detect the mime-type.

You can find a list of common mime-types here.

In PHP, you can use finfo_file () to detect the ime-type of the file.

Content-Disposition

This header tells the browser to open a file download window, instead of trying to parse the response content. For example:

Content-Disposition: attachment; filename="download.zip"

This will cause the browser to display such a dialog box:


Note that the Content-Type header information suitable for it will also be sent

Content-Type: application/zip
Content-Disposition: attachment; filename="download.zip"

Content-Length

When the content is to be transmitted to the browser, the server can use this header to inform the browser of the size (bytes) of the file to be transmitted ).

Content-Length: 89123

This information is quite useful for file downloads. This is why the browser knows the download progress.

For example, I wrote a virtual script to simulate a slow download.

// it's a zip file
header('Content-Type: application/zip');
// 1 million bytes (about 1megabyte)
header('Content-Length: 1000000');
// load a download dialogue, and save it as download.zip
header('Content-Disposition: attachment; filename="download.zip"');
// 1000 times 1000 bytes of data
for ($i = 0; $i < 1000; $i++) {
echo str_repeat(".",1000);
// sleep to slow down the download
usleep(50000);
}

The result will be as follows:


Now, I comment out the Content-Length header:

// it's a zip file
header('Content-Type: application/zip');
// the browser won't know the size
// header('Content-Length: 1000000');
// load a download dialogue, and save it as download.zip
header('Content-Disposition: attachment; filename="download.zip"');
// 1000 times 1000 bytes of data
for ($i = 0; $i < 1000; $i++) {
echo str_repeat(".",1000);
// sleep to slow down the download
usleep(50000);
}

The result is as follows:


This browser will only tell you how much you have downloaded, but will not tell you how much you need to download in total. The progress bar does not display the progress.

Etag

This is another header generated for caching. It looks like this:

Etag: "pub1259380237;gz"

The server may respond to the browser with each sent file. This value can contain the last modification date, file size, or file checksum. Browsing caches it with the received document. The following HTTP request will be sent when the browser requests the same file again:

If-None-Match: "pub1259380237;gz"

If the requested document Etag value is the same as it, the server will send a 304 status code instead of 2oo. And no content is returned. The browser loads the file from the cache.

Last-Modified

As the name suggests, this header indicates the last modification time of the document in GMT format:

Last-Modified: Sat, 28 Nov 2009 03:50:37 GMT

$modify_time = filemtime($file);
header("Last-Modified: " . gmdate("D, d M Y H:i:s", $modify_time) . " GMT");

It provides another caching mechanism. The browser may send such a request:

If-Modified-Since: Sat, 28 Nov 2009 06:38:19 GMT

We have discussed in the If-Modified-Since section.

Location

This header is used for redirection. If the response code is 301 or 302, the server must send this header. For example, when you access the http://www.nettuts.com, the browser will receive the following response:

HTTP/1.x 301 Moved Permanently
...
Location: http://net.tutsplus.com/
...

In PHP, You can redirect visitors in this way:
header('Location: http://net.tutsplus.com/');

Status Code 302 is sent by default. If you want to send status code 301, write it as follows:

header('Location: http://net.tutsplus.com/', true, 301);

Set-Cookie

When a website needs to set or update its cookie information, it uses the following header:

Set-Cookie: skin=noskin; path=/; domain=.amazon.com; expires=Sun, 29-Nov-2009 21:42:28 GMT
Set-Cookie: session-id=120-7333518-8165026; path=/; domain=.amazon.com; expires=Sat Feb 27 08:00:00 2010 GMT

Each cookie is used as a separate header. Note: Setting the cookie through js will not be reflected in the HTTP header.

In PHP, you can use the setcookie () function to set the cookie. PHP will send an appropriate HTTP header.

setcookie("TestCookie", "foobar");

It sends the following header information:

Set-Cookie: TestCookie=foobar

If no expiration time is specified, the cookie will be deleted after the browser is closed.

WWW-Authenticate

A website may send this header to verify the user. When the browser sees this response in the header, a pop-up window is opened.

WWW-Authenticate: Basic realm="Restricted Area"

It will look like this:


In the chapter of the PHP manual, a simple piece of code demonstrates how to do this with PHP:

if (!isset($_SERVER['PHP_AUTH_USER'])) {
header('WWW-Authenticate: Basic realm="My Realm"');
header('HTTP/1.0 401 Unauthorized');
echo 'Text to send if user hits Cancel button';
exit;
} else {
echo "

Hello {$_SERVER['PHP_AUTH_USER']}.

";
echo "

You entered {$_SERVER['PHP_AUTH_PW']} as your password.

";
}


Content-Encoding

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

Content-Encoding: gzip

In PHP, if you call the ob_gzhandler () function, this header will be automatically set.



Address: http://css9.net/all-about-http-headers/

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.