Different uses of headers in PHP (case)

Source: Internet
Author: User
Tags response code
This article for everyone to share the PHP header of the different usage summary (example), with a good reference value, I hope to be helpful to everyone.

1. The function of the header () is to send an original HTTP header [HTTP header] to the client.

Header (String,replace,http_response_code)/*string: Required. Specifies the string of headers to send. Replace: 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: Optional. Forces the HTTP response code to the specified value. */

Note: the header () function must be called before any actual output is sent.

2. Usage 1: Jump page

Header ("location:https://baidu.com"); Normal jump Header (' refresh:3; url=https://www.baidu.com '); 3s after jump//in the header for the jump, to avoid errors, the code continues to execute, generally add a exit;

Usage 2: declare content-type (which I often use to solve garbled characters)

Header (' Content-type:text/html;charset=utf-8 ');

Usage 3: return the response status code

Header (' http/1.1 403 Forbidden ');

Usage 4: perform the download operation (the location of the hidden file)

Header (' Content-type:application/octet-stream '); Set Content Type header (' content-disposition:attachment; filename= ' example.zip ');//set MIME user as Attachment header (' Content-transfer-encoding:binary '); Set the Transmit mode header (' Content-length: '. FileSize (' Example.zip ')); Set Content length

Usage 5: Control Browser caching

Header (' Expires:mon, Jul 1997 05:00:00 GMT '); If the page on the server changes frequently, set it to-1, which indicates an immediate expiration header (' last-modified: '. Gmdate (' d, D M Y h:i:s '). ' GMT '); header (' Cache-control:no-store, No-cache, Must-revalidate '); header (' Cache-control:post-check=0, pre-check= 0 ', false); header (' Pragma:no-cache ');

Usage 6:

3. More examples

<?php//okheader (' http/1.1 OK ');//Set a 404 Header: Header (' http/1.1 404 Not Found ');//Set address is permanently redirected header (' http/1.1 301 Moved permanently ');//Go to a new address header (' location:http://www.example.org/');//File Delay Turn: header (' Refresh:10; url=http:// www.example.org/');p rint ' You'll be being redirected in seconds ';//Of course, you can also use HTML syntax to implement//<meta http-equiv= "Refresh" Content= "10;http://www.example.org//>//override X-powered-by:php:header (' x-powered-by:php/4.4.0 '); Header (' x-powered-by:brain/0.6b ');//Document Language header (' content-language:en ');//Tell the browser the last modification time: +/-60; or Filemtime ($FN), Etcheader (' last-modified: '. Gmdate (' d, D M Y h:i:s ', $time). ' GMT ');//Tell the browser that the document content has not changed header (' http/1.1 304 not Modified ');//Set Content Length header (' content-length:1234 ');// Set to a download type header (' Content-type:application/octet-stream '); header (' content-disposition:attachment; Filename= ') Example.zip "'); Header (' content-transfer-encoding:binary ');//load the file to Send:readfile (' example.zip ');//Disable the cache header for the current document (' Cache-control:no-cAche, No-store, max-age=0, Must-revalidate '); header (' Expires:mon, Jul 1997 05:00:00 GMT '); Date in the Pastheader (' Pragma:no-cache ');//Set Content Type: Header (' content-type:text/html; charset=iso-8859-1 '); Header (' content-type:text/html; Charset=utf-8 '); header (' Content-type:text/plain '); Plain Text Format header (' Content-type:image/jpeg '); JPG Image header (' Content-type:application/zip '); Zip file header (' Content-type:application/pdf '); PDF file Header (' Content-type:audio/mpeg '); Audio file header (' Content-type:application/x-shockwave-flash '); Flash animation//Display Login Dialog header (' http/1.1 401 Unauthorized '); header (' Www-authenticate:basic realm= ' Top Secret "');p rint ' Text that would be displayed if the user hits cancel or ';p rint ' enters wrong login data ';? >

Related Article

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.