In Our previous article, we introduced the header definition, so a lot of people see the title will ask before the introduction is to send the original HTTP header to the client? Is that really the case? Today, let's take a look at the role of the header function in PHP.
First look at the definition of official documents
(PHP 4, PHP 5, PHP 7)
header- Sending native HTTP headers
1 void Header (string $string [, bool $replace = true [, int $http _response_code]])
Parameters:
String
There are two kinds of special heads. The first type of "http/" (case was not significant) will be used to calculate the HTTP status code that will be sent. For example, in the Apache server with a PHP script to handle the non-existent file request (using the errordocument instruction), you will want the script to respond to the correct status code.
1 <?php2 header ("http/1.0 404 Not Found"); 3?>
The second special case is the header information for "Location:". It not only sends the message to the browser, but also returns a status code of REDIRECT(302) to the browser, unless the status code has been set in advance for 201 or 3xx.
1 <?php2 header ("location:http://www.example.com/"); /* Redirect Browser */3 4/* Make sure this code below does not get executed when we Redirect. */5 exit;6?>
1 <?php2 header (' Www-authenticate:negotiate '); 3 header (' WWW-AUTHENTICATE:NTLM ', false); 4?>
Http_response_code
Forces the value of the specified HTTP response. Note that this parameter is valid only if the message string ( string
) is not empty.
The common use of the header function has the following points:
1. redirect
Header (' location:http://www.example.com/');
2. Specified content:
Header (' content-type:application/pdf ');
3. Accessories:
Header (' content-type:application/pdf '); Specifies the content as an attachment, specifying the name of the download display header (' content-disposition:attachment; filename= ' downloaded.pdf '); Open the file and output ReadFile (' original.pdf ')
The above code can be used in the browser generated file dialog box effect
4. Let users get the latest information and data instead of caching
Header ("Cache-control:no-cache, must-revalidate"); http/1.1 header ("Expires:sat, Jul 1997 05:00:00 GMT"); Set critical time
Detailed examples
<?phpheader (' http/1.1 OK '); OK normal access header (' http/1.1 404 Not Found '); Notification Browser page does not exist header (' http/1.1 301 Moved permanently '); Set the address to be permanently redirected 301header (' location:http://www.ithhc.cn/'); Jump to a new address header (' refresh:10; url=http://www.ithhc.cn/'); The delayed turn is the jump header (' x-powered-by:php/6.0.0 ') every few seconds; Modify the X-powered-by Information header (' Content-language:en '); Document Language header (' content-length:1234 '); Set the content Length header (' last-modified: '. Gmdate (' d, D M Y h:i:s ', $time). ' GMT '); Tells the browser the last time the header was modified (' http/1.1 304 not Modified '); Tells the browser that the document content has not changed # # #内容类型 # # #header (' content-type:text/html; Charset=utf-8 '); Page encoding header (' Content-type:text/plain '); Plain Text Format header (' Content-type:image/jpeg '); JPG, JPEG header (' Content-type:application/zip '); Zip file header (' Content-type:application/pdf '); PDF file Header (' Content-type:audio/mpeg '); Audio file header (' Content-type:text/css '); CSS file header (' Content-type:text/javascript '); JS file header (' Content-type:application/json '); Jsonheader (' Content-type:applicatIon/pdf '); Pdfheader (' Content-type:text/xml '); Xmlheader (' Content-type:application/x-shockw**e-flash '); Flash Animation ###### # # #声明一个下载的文件 # # #header (' Content-type:application/octet-stream '); header (' Content-disposition: Attachment Filename= "Itblog.zip"); header (' content-transfer-encoding:binary '); ReadFile (' test.zip '); ###### # # #对当前文档禁用缓存 # # # Header (' Cache-control:no-cache, No-store, max-age=0, Must-revalidate '); header (' Expires:mon, Jul 1997 05:00:00 GMT ') ###### # # #显示一个需要验证的登陆对话框 # # # # # header (' http/1.1 401 Unauthorized '); Header (' Www-authenticate:basic realm= ' Top Secret '); ###### # # #声明一个需要下载的xls文件 # # #header (' content-disposition:attachment; filename=ithhc.xlsx '); header (' Content-type: Application/vnd.openxmlformats-officedocument.spreadsheetml.sheet '); header (' Content-length: '. FileSize ('./ Test.xls ')); Header (' content-transfer-encoding:binary '); Header (' cache-control:must-revalidate '); Header (' Pragma:public '); ReadFile ('./test.xls '); ######?>
Summarize:
After reading this article believe that the small partners of PHP in the role of the header function, I hope that your work has helped!
Related recommendations:
A detailed description of header headers in PHP
PHP header function using method explained
The reason analysis and solution of PHP header invalidation
PHP Header Jump Detailed analysis