PHP Header function

Source: Internet
Author: User
Tags ranges setcookie

The Header () function-is used to send raw HTTP headers.1. redirect

<?php

Header ("location:http://www.jb51.net";);

Exit After each redirect, "Exit" must be added to avoid an error and continue execution.

?>

<?php

Header ("refresh:2;url=http://www.jb51.net";);

echo "Loading, please wait ...<br/> after three seconds auto Jump value <a

href= "Http://www.jb51.net" mce_href= "http://www.jb51.net" > Baidu </a> ";

?>

2. Prevent pages from being cached in IE

Not to get data from proxy or cache

<?php

Header (' Expires:fri, 4 Dec 09:00:00 GMT ');//Set Expiration time

Header (' last-modified: '. Gmdate (' d, D M Y h:i:s '). ' GMT ');

Header (' Cache-control:no-store,no-cache,must-revalidat ');

Header (' cache-control:post-check=0, pre-check=0 ', false);

Header (' Program:no-cache ');//compatible with http1.0 and HTTPS

?>

3. Let users not find the profile information

The header () function of 1php sends the status header to the browser

Header (' status:404 not Found ');

The browser returns status information.

2 Standard notation header (' http/1.0 404 Not Found ');

1. Part is the version number of the HTTP Protocol (http-version)

2. Status Code

3. Reason phrase (reason-phrase)

4. Download the file

HTML tags can be used to achieve ordinary file download, if for the sake of secrecy, you can not put the file link to tell others, you may use the header function for file download. The program sends header information that is used to tell Apache and the browser to download the information about the file.

<?php

function Download_by_path ($path _name, $save _name) {

Ob_end_clean ();

$hfile =fopen ($path _name, "RB") or Die ("Can not find File: $path _name\n");

Header ("Content-type:application/octet-stream");

Content-type means that the file MIME type is file stream format, set to the above form

The browser will know that this is a file stream format file and prompts the user to download it.

Header ("Content-transfer-encoding:binary");

Encoded in a binary way

Header ("Accept-ranges:bytes");

Accept-ranges is a response header that allows the server to indicate the offset and length that will be given in the

, an acceptance request for a resource component that is understood to be the unit of measure for the request scope.

Header ("Content-length:" FileSize ($path _name));

Content-length is the length of the byte that specifies the data contained in the request or response.

Header ("content-disposition:attachment;filename=\" $save _name\ "");

Content-disposition:attachment tells the browser that files can be downloaded as attachments,

The downloaded file name is $save the value of the _name variable.

while (!feof ($hfile)) {

Echo fread ($hfile, 1024*1024);

}

Fclose ($hfile);

}

HTML tag Download

<a href= "Web.rar" >download</a>

Browsers support the ability to open the format, it will be opened by default directly online (such as word or picture), unsupported format, it will pop up the download prompt. It is best to make the. rar format.

5. Output content before header function

In general, you cannot output HTML content before the header function, similar to the Setcookie () and session functions, which need to add message header information to the output stream.

The reason: When the php script starts executing, it can send both the HTTP message header (header) information and the principal information. The HTTP message header (from the header () or the Setcookie () function) is not sent immediately, instead it is saved to a list. This allows you to modify the header information, including the default header. However, once the script sends out any non-header output (for example, using HTML or print () calls), then PHP must first send all the headers and then terminate the HTTP header. Then continue to send the principal data. From this point on, any attempt to add or modify header information is not allowed.

Workaround: 1php.ini Open Cache (output_buffering)

2 using the Cache function Ob_start (), Ob_end_flush (), etc.

When the cache is started, PHP does not send an HTTP header when the script sends the output. Instead, it outputs this output to a dynamically increasing cache through a pipe. You can still modify/add a header, or set a cookie because the header is not actually sent. When all the scripts are terminated, PHP automatically sends the HTTP header to the browser and then sends the content in the output cache.

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.