PHP header () function syntax and using code _php tutorial

Source: Internet
Author: User
Tags response code browser cache
Grammar:
Copy CodeThe code is as follows:
Void Header (String $string [, bool $replace =true [, int $http _response_code)

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

Parameter description
string is required. Specifies the string of headers to send.
Replace is optional. Indicates whether the header replaces the previous header, or adds a second header.
The default 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. (Available in PHP 4 and later)
Examples of common usages:

First, the sending server status code

Copy the Code code as follows:
Header (' http/1.0 404 Not Found ');

Common status Code:

Status Code Description
100-199 indicates a successful receive request, requiring the client to continue submitting the next request to complete the process
200-299 successfully received the request and completed the entire 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, often 302,304
400-499 client request Error Common 404
500-599 Server side error, common 500
See more: Http&ftp corresponding tips

Second, page jump
Copy the Code code as follows:
Jump now
Header (' location:http://www.jb51.net/');

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

3. Set the browser cache

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

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, etc., 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 in the private cache only
No-cache all content is not cached
No-store All content is not cached to the cache or In the temporary Internet file
Must-revalidation/proxy-revalidation If the cached content fails, the request must be sent to the server/proxy for re-authentication
max-age=xxx (XXX is numeric) cached content will expire after xxx seconds, this option is only available in HTTP 1.1, and if used with last-modified, the higher priority
Open new Window
If the value specified Cache-control is private, No-cache, Must-revalidate, the server will be re-accessed when a new window is opened. If you specify a max-age value, the time within this value does not re-access the server, such as the following:
Copy Code code is 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 the Address bar
If the value is private or must-revalidate, the server is accessed only on the first visit and is not accessed at a later time. If the value is No-cache, it is accessed every time. If the value is Max-age, access is not repeated until it expires.
Press the Back button
If the value is private, must-revalidate, max-age, it is not re-accessed, and if it is no-cache, it is repeated every time
Press the Refresh button
No matter what the value, it will be accessed repeatedly. When you specify a Cache-control value of "No-cache", accessing this page does not leave a page backup in the Temporary Internet Article folder. Also, the cache is affected by specifying a value of "Expires". For example, to specify that the expires value is a long past time, then when you visit this network repeatedly press ENTER in the Address bar, then each time will be repeated access:
Expires:fri, Dec 1999 16:00:00 GMT
About "last-modified
When the browser requests a URL for the first time, 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 end of the service period, similar in format:
Last-modified:fri, 2006 18:53:33 GMT
When the client requests this URL for the second time, according to the HTTP protocol, the browser transmits the If-modified-since header to the server, asking if the file has been modified after that time:

If-modified-since:fri, 2006 18:53:33 GMT
If the server-side resource does not change, the HTTP 304 (not Changed.) Status code is returned automatically, and the content is empty, which saves the amount of data transferred. When the server-side code changes or restarts the server, the resource is re-emitted, similar to when the first request is returned. This ensures that the 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 will use the HTTP header for Last-modified and ETAGS requests, which can take advantage of caching by clients such as browsers. Because the server first generates the LAST-MODIFIED/ETAG tag, the server can later use it to determine if the page has been modified. Essentially, the client requires the server to validate 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 last-modified
The customer requests page A again and passes the last-modified that the server returned when the last request was sent to the server
The server examines 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 the 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 attachment, to name and download
Header (' content-disposition:attachment; filename= ' downloaded.pdf ');

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

In fact, it is better to understand these!
Copy the 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);

http://www.bkjia.com/PHPjc/825013.html www.bkjia.com true http://www.bkjia.com/PHPjc/825013.html techarticle Syntax: Copy code code as follows: Void header (String $string [, bool $replace =true [, int $http _response_code) Send the original HTTP header to the client note: header letter The number must be in ...

  • 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.