Phpheader () function syntax and code

Source: Internet
Author: User
The Phpheader () function syntax and usage code are described in detail. Common status codes of headers are listed here.

The Php header () function syntax and usage code are described in detail. Common status codes of headers are listed here.

Syntax:

The Code is as follows:


Void header (string $ string [, bool $ replace = true [, int $ http_response_code)


Send the original HTTP header to the client
Note:
The Header function must be called before any actual output, whether it is a common html Tag, a hollow line of a file, or from php. That is, there cannot be any form of output before this function.
Parameter description:

Parameter description
String is required. Specifies the header string to be sent.
Replace is optional. Indicates whether the header replaces the previous header or adds the second header.
The default value is true (replace ). False (multiple headers of the same type are allowed ).

Http_response_code is optional. Forces the HTTP response code to the specified value. (PHP 4 and later versions are available)
Examples of common usage:

I. Sending Server Status Code

The Code is as follows:


Header ('HTTP/1.0 404 Not Found ');


Common status codes:

Status Code Description
100-199 indicates that the request is successfully received, and the client is required to submit the next request to complete the processing.
200-299 successfully received the request and completed the entire process.
300-399 incomplete requests, the client needs to further refine the request, for example, the requested resource has been moved to a new address, usually 302,304
400-499 client request errors 404
500-599 server errors, commonly used 500
For details, see the HTTP & FTP prompts.

Ii. page Jump

The Code is as follows:


// Jump now
Header ('location :');

// Jump 3 seconds later
Header ('refresh: 3; url = http://www.jb51.net ');

3. Set browser cache

Force the browser not to cache!
[Code]
// Header ('expires:-1 ');
Header ('expires: Mon, 26 Jul 1997 05:00:00 gmt ');
Header ('cache-Control: no-Cache, must-revalidate ');
Header ('pragma: no-cache ');
Header ('Last-Modified: '. date ('d, j m y h: I: s t '));


Note:
The time after Expires must be in GMT format, for example: gmdate ("M d y h: I: s ");
Common Cache-control values include private, no-cache, max-age, and must-revalidate. The default value is private. Its function is divided into the following situations based on different re-browsing methods.
Cache-directive description
All public content will be cached
Private content is only cached in private Cache
No-cache all content will not be cached
No-store does not cache all content in cache or Temporary Internet Files.
Must-revalidation/proxy-revalidation if the cached content is invalid, the request must be sent to the server/proxy for re-verification.
Max-age = xxx (xxx is numeric) the cached content will expire after xxx seconds. This option is only available in HTTP 1.1. If it is used together with Last-Modified, high priority
Open new window
If the cache-control values are private, no-cache, and must-revalidate, the server will be accessed again when a new window is opened. If the max-age value is specified, the server will not be accessed again within this value, for example:

The Code is as follows:


Header ('cache-control: max-age = 5 ');


Indicates that the server will not be accessed again within 5 seconds after the webpage is accessed.

Press enter in the address bar
If the value is private or Must-revalidate, the server will be accessed only during the first access and will not be accessed in the future. If the value is no-cache, it will be accessed every time. If the value is max-age, the access will not be repeated before expiration.
Press the back button
If the values are private, must-revalidate, and max-age, no re-access will be performed. If the value is no-cache, the access will be repeated each time.
Press refresh
No matter what the value is, it will be accessed again. When the Cache-control value is "no-cache", accessing this page will not leave page backup in the temporary article folder on the Internet. In addition, specifying the "Expires" value also affects the cache. For example, if you specify the Expires value as a time that has passed, if you repeatedly press enter in the address bar when accessing this network, the access will be repeated each time:
Expires: Fri, 31 Dec 1999 16:00:00 GMT
About "Last-Modified
When the browser requests a URL for the first time, the server returns 200, and the content is the resource you requested, at the same time, there is a Last-Modified attribute to mark the Last modification time of this file on the service end. The format is similar to this:
Last-Modified: Fri, 12 May 2006 18:53:33 GMT
When the client requests this URL for the second time, according to the HTTP protocol, the browser will send the If-Modified-Since header to the server, asking If the file has been Modified after the time:

If-Modified-Since: Fri, 12 May 2006 18:53:33 GMT
If the resources on the server are Not Changed, the HTTP 304 (Not Changed.) status code is automatically returned and the content is blank, which saves the amount of data transmitted. When the server code changes or the server is restarted, the resource is re-issued, and the returned result is similar to the first request. This ensures that resources are not repeatedly sent to the client, and that the client can obtain the latest resources when the server changes.

Last-Modified improves performance
Smart developers will use the http header of the Last-Modified and ETags requests together, so that the cache of the client (such as the browser) can be used. Because the server first generates the Last-Modified/Etag mark, the server can use it later to determine whether the page has been Modified. Essentially, the client sends this mark back to the server and requires the server to verify its (client) cache.

The process is as follows:

Client requests A page ()
The server returns page A and adds A Last-Modified
The client displays the page and caches the page together with Last-Modified.
The customer requests page A again and passes the Last-Modified returned by the server in the Last request to the server.
The server checks the Last-Modified and determines that the page has not been Modified since the Last client request. The server returns the 304 response and an empty response body.

Iii. File Download
For example, download a PDF file

The Code is as follows:


// We'll be outputting a PDF
Header ('content-type: application/pdf ');

// It will be called downloaded.pdf
// Indicates that it is an attachment, name it, and download it
Header ('content-Disposition: attachment; filename = "downloaded.pdf "');

// The PDF source is in 1.pdf
Readfile('1.pdf ');


In fact, we need to better understand this!

The Code is as follows:


Header ('content-type: application/octet-sream ');
Header ('Accept-Ranges: bytes ');

// Return byte
Header ('content-disposition: attachement; filename = "file name "');
Header ('Accept-length: '. $ filesize );

Related Article

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.