What are the functions of the PHP header function? Introduction to the PHP header function (with code)

Source: Internet
Author: User
Tags readfile
What's the function of the PHP header function? In PHP, the header function can be used as two special headers to send the status code, you can replace the previous same type of header, you can also force specify the value of the HTTP response, then we will take a look at the specific content.

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?>

 Replace

    • Optional Parameters replace indicates whether to replace the previous header of the same type with the following header. is replaced by default. If passed FALSE in, you can force the same header information to coexist. For example:

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 ');

Specify 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 '); ######?>

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.