PHP header detailed instructions and experience

  • 2020-03-31 16:43:17
  • OfStack

No matter how many headers there are on the page, it will execute the last one, but with conditions, such as:
The header (" Location://www.jb51.net ");
The header (' Location: http://www.g.cn);
The header (" Location:http://www.baidu.com ");
This will jump to baidu
The header (" Location://www.jb51.net "); Echo 'this site ';
The header (' Location: http://www.g.cn);
The header (" Location:http://www.baidu.com ");
This will jump to Google

The following is a detailed description of the use of the header function
I. functions:
~ ~ ~ ~ ~ ~ ~ ~ ~
PHP simply sends the header of the HTML document to the browser using the HTTP protocol, telling the browser exactly what to do with the page
A traditional header must contain one of the following three headers and can occur only once.
Location: XXXX: yyyy/ZZZZ
The content-type: XXXX/yyyy
Status: NNN XXXXXX

Let's first understand how the HTTP protocol works
~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~
The HTTP protocol is based on the request/response paradigm. After a client establishes a connection with the server, it sends a request to the server in the format of uniform resource identifier, protocol version number, followed by MIME information including request modifiers, client information, and possible content. Upon receiving the request, the server gives the corresponding response information in the form of a status line containing the protocol version number of the message, a success or error code, followed by MIME information including the server information, entity information, and possible content.
It is divided into four procedures, in the HTTP protocol, the server refers to the part of the HTTP service, the client refers to the browser you use or download tools, and so on. During the communication, the client side issues a request for connection and the server side establishes a connection. The client then makes an HTTP Request and the server returns a response, thus completing an HTTP operation.

Three, the meaning of the HTTP protocol status code
~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~
1 x x
2 X x indicates that the request was received successfully
The customer needs to further refine the request to complete the request
4 X x customer error
5 X x server error

Iv. Operation examples:
~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~
< 1 > Redirection is the most common
< ? PHP
The Header (" Location: http://www.php.net ");
? >

< 2 > Instead of using the existing client cache, force the user to get the latest information every time they visit the page.
< ? PHP
// tells the browser that the page is out of date (Greenwich mean time), as long as the date has passed.
Header ("Expires: Mon, 26 Jul 1970 05:00:00 GMT");
// the purpose of telling the browser that the page was last updated (in Greenwich mean time) on the same day is to force the browser to get the latest information
Header (" last-modified: ". Gmdate ("D, D M Y H: I :s"). "GMT");
// tells the client browser not to use the cache
Header (" cache-control: no - Cache, must - revalidate ");
// parameter (compatible with previous servers), which is compatible with the HTTP1.0 protocol
The header (" Pragma: no cache - ");
// output MIME type
The content-type header (" : application/file ");
// file length
The header (" the Content - Length: 227685 ");
// accepted units of scope
The header (" Accept - Ranges: bytes ");
// file name in the file save dialog box by default
The header (" the Content - Disposition: attachment; Filename = $filename ");
? >

< 3 > Output status values to the browser, mainly for access control
< ? PHP
The header (' 401 Unauthorized HTTP / 1.1);
The header (' status: 401 Unauthorized ');
? >
For example, to restrict a user's access to the page, you can set the status to 404, as shown below, so that the browser shows that the page does not exist
< ? PHP
The header (' HTTP / 1.1 404 Not Found ').
The header (" status: 404 Not Found ");
? >

Note: traditional headers must contain one of the following three headers and can occur only once. Contenttype: XXXX /yyyy Location: XXXX :yyyy/ ZZZZ Status: NNN XXXXXX can appear more than twice on the new Multipart MIME side.

Use examples

Example 1: this example redirects the browser to the official PHP website.
The Header (" Location: http://www.php.net "); The exit; > ?
Example 2: to ensure that the user gets the latest data each time, instead of the data in the Proxy or cache, use the following headers
Header ("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
Header (" last-modified: ". Gmdate ("D, D M Y H: I :s"). "GMT"); Header (" cache-control: no - Cache, must - revalidate ");
The header (" Pragma: no cache - "); > ?

Example 3: make the user's browser display information that cannot be found in the file.
The header (" Status: 404 Not Found "); > ?

Example 4: allow users to download files.
The content-type header (" : application/x - gzip ");
The header (" the Content - Disposition: attachment; Filename = ");
The header (" the Content - Description:.php3 Generated Data "); > ?

Header -- sends a raw HTTP header description
Void header (string string [, bool replace [, int http_response_code]])

The header() function is used to send a raw HTTP header. For more on HTTP headers, see the HTTP/1.1 specification.
The optional parameter replace specifies whether to replace the previous similar header or to add a header of the same type. By default, replace, but if set to FALSE, you can force multiple headers of the same class to be sent. Such as:

< ? PHP
The header (' WWW - Authenticate: Negotiate ');
The header (' WWW - Authenticate: NTLM, false);
? >

The second optional parameter, http_response_code, forces the HTTP response code to be set to the specified value (this parameter is new to PHP 4.3.0).
There are two special header calls. The first is a header that begins with the string "HTTP/" (case doesn't matter) and can be used to determine the HTTP status code to send. For example, if you configure Apache to use PHP to handle error-handling requests for missing files (using the ErrorDocument directive), you need to make sure that the script produces the correct status code.

< ? PHP
The header (" HTTP / 1.0 404 Not Found ")
? >
Note: the HTTP status code header line is always the first to be sent to the client, regardless of whether the actual header() call is the first. Unless the HTTP header has been sent, you can always override the original by calling the header() function with the new status line.

The second special case is the "Location:" header. Not only does it send the tag back to the browser, it also sends a REDIRECT (302) status code back to the browser, unless a 3xx status code has been sent before.
< ? PHP
The header (" Location: http://www.example.com/ ");

The exit;
? >
Note: the HTTP/1.1 standard requires a URI of an absolute address as an argument to Location:, but some clients support relative uris. You can usually use the functions $_SERVER['HTTP_HOST'], $_SERVER['PHP_SELF'], and dirname() to generate an absolute URI from a relative URI:

< ? PHP
The header (" Location: http:// ". $_SERVER [' HTTP_HOST]
The rtrim (dirname ($_SERVER [' PHP_SELF ']), '/ \ \')
. "/". $relative_url);
? >

Note: even if session.use_trans_sid is enabled, the session ID is not passed along with the Location header. You must pass it manually using the SID constant.

PHP scripts typically produce dynamic content that must not be cached by browsers or proxy servers. Many proxy servers and browsers can be disabled by:

< ? PHP
Header (" cache-control: no - Cache, must - revalidate "); / / HTTP / 1.1
Header ("Expires: Mon, 26 Jul 1997 05:00:00 GMT"); // past time
? >

Note: you may find that the page is not buffered even if you do not output all of the above code. Users have a number of options to set to change the browser's default caching behavior. By sending the header above, you should be able to override any Settings that cause the script page to be cached.

In addition, when session is used, the session_cache_limiter() function and the session.cache_limiter option can be used to automatically generate the correct cache-related headers.

Remember that header() must be called before any actual output, whether from a plain HTML tag, a blank line, or PHP. A common mistake is that when code is read through include(), require(), or some other file access class function, some whitespaces or empty lines are sent out before header() is called. This error is also common in a single PHP/HTML file.

< Html>
< ? PHP

The header (' Location: http://www.example.com/);
? >

Note: since PHP 4, you can solve this problem with some output buffering functions. The price is to cache all the output to the browser on the server until the command sends it. You can do this using ob_start() and ob_end_flush() in your code, by modifying the output_buffering configuration option in php.ini, or by modifying the server configuration file.

Related articles: