Summary of usage of PHP header

Source: Internet
Author: User

This article makes a summary of the function and usage of header function in PHP language.

The specific function of the PHP header () function is to send a raw HTTP header [HTTP header] to the client.

Headers (header) is a string of characters sent by the server before the HTTP coprocessor to the browser, and a blank line is separated between the header and the HTML file. Call the header () function before any actual output is sent (in PHP 4 and later, you can use output caching to solve the problem).

Here are some of the more common header usage examples:

(1) Use header to implement redirection (equivalent to entering a URL in the address bar instead of the user).

<?php
Header ("location:http://www.alixixi.com/mysql/407.html";); 
exit;//Note that you must add exit after each redirect, and the code will continue to execute downward after the error occurs
?>
<?php 
/** 
@title:P HP Implementation timer Jump 
@ function: Wait for the specified time, and then jump to the specified page (instead of HTML meta method) 
*/ 
header ("REFRESH:3; Url=http://www.alixixi.com/javascript-function/605.html "); 
Echo ' is loading, please wait ...<br> three seconds to automatically jump ';
/* 
Description: If the wait time is 0, it is equivalent to the header ("Location:"). 
* *
?>

(2) Prevent the page from being cached in the browser.

The following headers can be used if a visitor browses the page every time because the business requires it to get the latest information, instead of the data in Proxy or cache:

<?php
Header (' Expires:mon, 1997 05:00:00 GMT ');
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 '); Compatible http1.0 and HTTPS
?>

The main thing here is to set the values of the following options:

CacheControl = No-cache
Pragma=no-cache
Expires =-1

When the HTTP1.1 server specifies CacheControl = No-cache, the browser does not cache the Web page. Legacy HTTP 1.0 Servers cannot use Cache-control headers. So for backward compatibility with HTTP 1.0 servers, IE uses the Pragma:no-cache header to provide special support for HTTP.

For a specific introduction to Cache-control, please refer to this site article:

Web page cache Control Cache-control common values have private, No-cache, Max-age, Must-revalidate Introduction

Internet Explorer does not cache this response if the client communicates with the server through a secure connection (https://) and the server returns Pragma:no-cache headers in the response. Note: Pragma:no-cache only prevents caching when used in a secure connection, and if used in an unsecured page, the processing is the same as expires:-1, and the page is cached but marked as immediately expired.

Expires is a good thing, if the Web page on the server often changes, set it to-1, indicating immediate expiration. If a page is updated daily 1 o'clock in the morning, you can set the expires to 1 o'clock in the morning the next day.

(3) The user's browser appears to be unable to find the file information.

A lot of information on the Internet to write: Php function header () can send the Status header to the browser, such as header ("status:404 not Found"). But I found that the response returned by the browser was:

http/1.x OK
Date:thu, Aug 2006 07:49:11 GMT
server:apache/2.0.55 (WIN32) php/5.0.5
x-powered-by:php/5.0.5
status:404 not Found
content-length:0
Keep-alive:timeout=15, max=98
Connection:keep-alive
Content-type:text/html

After some information, the correct wording is:

Header ("http/1.1 404 Not Found");

The first part is the version of HTTP Protocol (http-version), the second part is state code (status), and the third part is the reason phrase (reason-phrase).

(4) Let visitors download the file (the location of hidden files).

We all know that through the original URL can be used to download ordinary files, but if we need confidential files on the server storage location, you can not directly display the file link, here we can through the header function to hide the file address at the same time implementation of the file download, the specific code is as follows:

<?php
Header ("Content-type:application/x-gzip"); 
Header ("content-disposition:attachment; Filename=alixixi.zip "); 
Header ("Content-description:php3 generated Data"); 

In addition, on the use of php file download, you can refer to this site article:

PHP File Download function (code)

Here are some examples of usage that are not very common headers:

<?php

//OK
header (' http/1.1 ok ');

Set a 404 Header:
header (' http/1.1 404 Not Found ');

Set address to be permanently redirected
header (' http/1.1 moved Permanently ');

Document language
header (' content-language:en ');

Tell the browser when the last modification
$time = time ()-A//or Filemtime ($FN), etc
header (' last-modified: '. Gmdate (' d, D M Y h:i:s ', $time). ' GMT ');

Tells the browser that the contents of the document have not changed
header (' http/1.1 304 not Modified ');

Set the content length
header (' content-length:1234 ');

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


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.