The main functions of PHP header function

Source: Internet
Author: User
Tags readfile

First look at the definition of official documents

(PHP 4, PHP 5, PHP 7)

header- Sending native HTTP headers

1 Header string $string $replace true $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 <? PHP 2 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  <?< Span style= "COLOR: #000000" >php  2  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 <? PHP 2 Header (' Www-authenticate:negotiate '); 3 Header 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:

1<?PHP2 Header(' http/1.1-OK ');//OK normal Access3 Header(' http/1.1 404 Not Found ');//notifies the browser that the page does not exist4 Header(' http/1.1 301 Moved permanently ');//set the address to be permanently redirected 3015 Header(' location:http://www.ithhc.cn/');//jump to a new address6 Header(' refresh:10; url=http://www.ithhc.cn/');//delayed steering is a few seconds to jump7 Header(' x-powered-by:php/6.0.0 ');//Modify x-powered-by Information8 Header(' Content-language:en ');//Document Language9 Header(' content-length:1234 ');//Set Content LengthTen Header(' last-modified: '.gmdate(' d, D M Y h:i:s ',$time).‘ GMT ');//tell the browser when it was last modified One Header(' http/1.1 304 not Modified ');//Tell the browser that the contents of the document have not changed A   - ## #内容类型 # # # - Header(' content-type:text/html; Charset=utf-8 ');//page Encoding the 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 Files - Header(' Content-type:text/css ');//css File + Header(' Content-type:text/javascript ');//js file A Header(' Content-type:application/json ');//JSON at Header(' content-type:application/pdf ');//PDF - Header(' Content-type:text/xml ');//XML - Header(' Content-type:application/x-shockw**e-flash ');//Flash Animation -   - ###### -   in ## #声明一个下载的文件 # # # - Header(' Content-type:application/octet-stream '); to Header(' Content-disposition:attachment; filename= "Itblog.zip"); + Header(' Content-transfer-encoding:binary '); - ReadFile(' Test.zip '); the ###### *   $ ## #对当前文档禁用缓存 # # #Panax Notoginseng Header(' Cache-control:no-cache, No-store, max-age=0, Must-revalidate '); - Header(' Expires:mon, Jul 1997 05:00:00 GMT '); the ###### +   A ## #显示一个需要验证的登陆对话框 # # # the Header(' http/1.1 401 Unauthorized ');  + Header(' Www-authenticate:basic realm= ' Top Secret ');  - ###### $   $   - ## #声明一个需要下载的xls文件 # # # - Header(' Content-disposition:attachment; Filename=ithhc.xlsx '); the Header(' Content-type:application/vnd.openxmlformats-officedocument.spreadsheetml.sheet '); - Header(' Content-length: '.filesize('./test.xls ')); Wuyi Header(' Content-transfer-encoding:binary ');  the Header(' Cache-control:must-revalidate ');  - Header(' Pragma:public ');  Wu ReadFile('./test.xls ');  - ###### About?>

The main functions of PHP header function

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.