PHP header () function syntax and _php tips for using code

Source: Internet
Author: User
Tags response code browser cache

Grammar:

Copy Code code as follows:

Void Header (String $string [, bool $replace =true [, int $http _response_code)

Send the original HTTP header to the client
Need to note:
The header function must be invoked before any actual output, whether it is a generic HTML tag, a file hollow line, or from PHP. It is not possible to have any form of output before this function.
Parameter description:

Parameter description
String required. Specify the header string to send.
Replace is optional. Indicates whether the header replaces the previous header, or adds a second header.
The default is true (replace). False (allows multiple headers of the same type).

Http_response_code Optional. Forces the HTTP response code to the specified value. (PHP 4 and later available)
Common usage Examples:

First, send server status code

Copy Code code as follows:

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

Common status Code:

Status Code Description
100-199 indicates a successful receipt of the request, requiring the client to continue submitting the next request to complete the process
200-299 successfully received the request, and has completed the entire processing process, commonly used 200
300-399 the request is not completed, the client needs to further refine the request, for example, the requested resource has been moved to a new address, commonly used 302,304
400-499 client request Error Common 404
500-599 Server side error, common 500
See more: Http&ftp corresponding prompt information

Second, page jump

Copy Code code as follows:

Jump now
Header (' location:http://www.jb51.net/');

3 Seconds to jump
Header (' refresh:3;url=http://www.jb51.net ');

3. Set Browser cache

Force browser not to cache!
[Code]
Header (' expires:-1 ');
Header (' Expires:mon, June 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 '));


Description
The time after expires must be in GMT format such as: Gmdate ("M D Y h:i:s");
Cache-control common values are private, No-cache, Max-age, must-revalidate and so on, the default is private. Its function is divided into the following situations according to different ways of re browsing.
Cache-directive description
Public all content will be cached
Private content is cached only in the privacy cache
No-cache All content is not cached
No-store All content is not cached in cache or temporary Internet files
Must-revalidation/proxy-revalidation if the cached content fails, the request must be sent to the server/agent for re-verification
Max-age=xxx (XXX is numeric) cached content will expire after xxx seconds, which is only available in HTTP 1.1 and has higher precedence if used with last-modified
Open a new window
If the specified Cache-control value is 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, then the time within this value does not regain access to the server, such as:
Copy Code code as follows:

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

Indicates that the server will not be accessed again within 5 seconds of accessing this page

Enter in Address bar
If the value is private or must-revalidate, the server is accessed only on the first visit and is not accessed later. If the value is No-cache, it is accessed every time. If the value is Max-age, the access is not repeated until it expires.
Press the Back button
If the value is private, must-revalidate, Max-age, it will not be accessed, and if No-cache, repeat access each time
Press the Refresh button
No matter what the value, it will be repeatedly accessed. When the specified Cache-control value is "No-cache", accessing this page does not leave a page backup on the Internet temporary article folder. In addition, caching can also be affected by specifying a "Expires" value. For example, to specify that the expires value is a long past time, if you repeatedly press ENTER in the Address bar when you visit the network, you will repeat the visit each time:
Expires:fri, Dec 1999 16:00:00 GMT
About "last-modified
When the browser first requests a URL, the server-side return status is 200, the content is the resource you requested, and there is a last-modified attribute that marks the last time the file was modified at the service end, similar in format:
Last-modified:fri, May 2006 18:53:33 GMT
The second time the client requests this URL, according to the HTTP protocol, the browser sends the If-modified-since header to the server asking if the file has been modified after that time:

If-modified-since:fri, May 2006 18:53:33 GMT
If the server-side resources do not change, the HTTP 304 (not Changed.) Status code is automatically returned, and the contents are empty, thus saving the amount of data transferred. When the server-side code changes or restarts the server, the resource is issued again, similar to the first request. This ensures that resources are not duplicated to the client and that the client is able to get the latest resources when the server changes.

Last-modified Improve Performance
Smart developers use the HTTP headers of last-modified and ETAGS requests, which can take advantage of caching by clients such as browsers. Because the server first generates a Last-modified/etag tag, the server can use it later to determine if the page has been modified. Essentially, the client requires the server to authenticate its (client) cache by passing the token back to the server.

The process is as follows:

Client requests a page (a)
The server returns page A and adds a last-modified to a
The client presents the page and caches the page along with the last-modified
The customer requests page A again and passes the last-modified returned by the server to the server together with the last request
The server checks the last-modified and determines that the page has not been modified since the last client request, directly returning response 304 and an empty response body.

Third, File download
Like downloading a PDF file

Copy Code code as follows:

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

It'll be called downloaded.pdf
Indicate that he is an accessory, to name and download
Header (' content-disposition:attachment; filename= "Downloaded.pdf");

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

In fact, to better understand these!
Copy Code code as follows:

Header (' Content-type:application/octet-sream ');
Header (' accept-ranges:bytes ');

Return by byte
Header (' content-disposition:attachement;filename= ' filename ');
Header (' accept-length: '. $filesize);

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.