Home  >  Article  >  Backend Development  >  Php header() function syntax and usage code_PHP tutorial

Php header() function syntax and usage code_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 10:25:35809browse

Syntax:

Copy code 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, regardless of It is a normal html tag, a blank line in the file, or it comes from php. That is, there cannot be any form of output before this function.
Parameter description:

Parameter Description
string Required. Specifies the header string to be sent.
replace is optional. Indicates whether this header replaces the previous header, or adds a second header.
Default is true (replacement). false (allow multiple headers of the same type).

http_response_code is optional. Forces the HTTP response code to the specified value. (Available in PHP 4 and above)
Common usage examples:

1. Sending server status code

Copy 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 has been successfully received, and the client is required to continue submitting the next request to complete the entire processing process.
200-299 indicates that the request has been successfully received and the entire processing process has been completed. 200 is commonly used
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 are 302,304
400-499. The client request is incorrect. Commonly used are 404
500- 599 An error occurred on the server side, commonly used 500
For details, please see: HTTP&FTP corresponding prompt information

2. Page jump

Copy code The code is as follows:

// Jump now Redirect
header('Location: http://www.jb51.net/');

// Redirect after 3 seconds
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, such as: gmdate("M d Y H:i:s");
Cache-control common The values ​​include private, no-cache, max-age, must-revalidate, etc. The default is private. Its function is divided into the following situations according to different re-browsing methods.
Cache-directive Description
public All content will be cached
private content will only be cached in the private cache
no-cache All content will not be cached
no-store All content will be cached Will not be cached 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 revalidation
max-age=xxx (xxx is numeric ) The cached content will expire after xxx seconds. This option is only available in HTTP 1.1, and if used together with Last-Modified, it has a higher priority
Open a new window
If the value of cache-control is specified private, no-cache, must-revalidate, then the server will be accessed again when a new window is opened. And if the max-age value is specified, the server will not be accessed again within this value, such as:
Copy code The code is as follows:

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

Indicates that the server will not be accessed again within 5 seconds after accessing this webpage

Press Enter in the address bar
If the value is private or Must-revalidate, the server will only be accessed the first time, 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, it will not be accessed again before expiration.
Press the back button
If the value is private, must-revalidate, max-age, it will not be accessed again, and if it is no-cache, it will be accessed again every time
Press the refresh button
Regardless of the value, it will be accessed repeatedly. When the Cache-control value is specified as "no-cache", accessing this page will not leave a page backup in the Internet temporary article folder. In addition, caching can also be affected by specifying the "Expires" value. For example, if you specify the Expires value as a time that has long passed, then if you press Enter repeatedly in the address bar when accessing this website, the access will be repeated every time:
Expires: Fri, 31 Dec 1999 16:00:00 GMT
About "Last-Modified
When the browser requests a URL for the first time, the return status from the server will be 200, the content is the resource you requested, and there is a Last-Modified attribute to mark this file in the service The time when the end of the period was last modified, 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 provisions of the HTTP protocol, the browser The server will send an If-Modified-Since header to the server, asking whether the file has been modified after this time:

If-Modified-Since: Fri, 12 May 2006 18:53:33 GMT
If the server-side resource has not changed, it will automatically return HTTP 304 (Not Changed.) status code with empty content, thus saving the amount of data transferred. When the server-side code changes or the server is restarted, the resource is reissued and the return is similar to the first request. This ensures that resources are not sent to the client repeatedly, and also ensures that when the server changes, the client can get the latest resources.

Last-Modified improves performance
Smart developers will use Last-Modified together with the http header of the ETags request, which can take advantage of the cache of the client (such as the browser). Because the server generates the Last-Modified/Etag tag first, the server can use it later to determine whether the page has been modified. Essentially, the client is asking the server to validate its (client-side) cache by passing this token back to the server.

The process is as follows:

The client requests a page (A)
The server returns page A and adds a Last-Modified to A
The client displays the page and caches the page together with Last-Modified
The client 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, and directly Returns a 304 response and an empty response body.

3. File download
For example, download a pdf file

Copy the code The code is as follows:

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

// It will be called downloaded.pdf
//Indicate it 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, you can understand these better this way!
Copy code The code is as follows:

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

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

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/825013.htmlTechArticle Syntax: Copy the code as follows: Void header(string $string[,bool $replace=true [, int $ http_response_code) When sending the original HTTP header to the client, please note: The Header function must be in...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn