PHP Header Use detailed

Source: Internet
Author: User
Tags php website
    1. Header ("Location:http://www.php.net");
    2. ?>
Copy Code

<2> forces users to get the latest information each time they access the page, rather than using a cache that exists on the client.

    1. Tell the browser how long this page will expire (in Greenwich Mean Time), as long as the date has passed.
    2. Header ("Expires:mon, Jul 1970 05:00:00 GMT");
    3. Tells the browser that the last update date of this page (in Greenwich Mean Time) is the same day, the purpose is to force the browser to get the latest information
    4. Header ("last-modified:".) Gmdate ("D, D M Y h:i:s"). "GMT");
    5. Tell the client browser not to use the cache
    6. Header ("Cache-control:no-cache, must-revalidate");
    7. Parameters (compatible with previous servers), which is compatible with the HTTP1.0 protocol
    8. Header ("Pragma:no-cache");
    9. Output MIME type
    10. Header ("Content-type:application/file");
    11. File length
    12. Header ("content-length:227685");
    13. Accepted range units
    14. Header ("Accept-ranges:bytes");
    15. File name in the File Save dialog box by default
    16. Header ("content-disposition:attachment; Filename= $filename ");
    17. ?>
Copy Code

<3> output status value to the browser, mainly for access rights control

    1. Header (' http/1.1 401 Unauthorized ');
    2. Header (' status:401 unauthorized ');
    3. ?>
Copy Code

For example, to restrict a user from accessing the page, you can set the status to 404, as shown below, so that the browser is displayed as if the page does not exist

    1. Header (' http/1.1 404 Not Found ');
    2. Header ("status:404 not Found");
    3. ?>
Copy Code

Note: The traditional header must contain one of the following three headers and can only occur once. CONTENT-TYPE:XXXX/YYYY location:xxxx:yyyy/zzzz status:nnn xxxxxx can appear more than two times in the new multi-header specification (Multipart MIME). The above is about the header information of the PHP content introduction, more information can be consulted: the header of the PHP file is detailed.

Give some concrete examples.

Example 1: This example redirects the browser to the official PHP website.

    1. Header ("Location:http://www.php.net"); Exit
Copy Code

Example 2: The user can get the latest information each time, instead of the Proxy or cache data, you may use the following headers

    1. Header ("Expires:mon, Jul 1997 05:00:00 GMT");
    2. Header ("last-modified:".) Gmdate ("D, D M Y h:i:s"). "GMT"); Header ("Cache-control:no-cache, must-revalidate");
    3. Header ("Pragma:no-cache");
Copy Code

Example 3: Let the user's browser appear unable to find the file information.

    1. Header ("status:404 not Found");
Copy Code

Example 4: Let the user download the file.

    1. Header ("Content-type:application/x-gzip");
    2. Header ("content-disposition:attachment; Filename= file name ");
    3. Header ("Content-description:php3 Generated Data");
Copy Code

Note: No matter how many headers the page has, it will execute the last one, but conditionally, for example:

    1. Header (' location:http://bbs.it-home.org ');
    2. Header (' location:http://www.g.cn ');
    3. Header (' location:http://www.baidu.com ');
    4. Skip to Baidu
    5. Header (' location:http://bbs.it-home.org '); Echo ' Programmer's house ';
    6. Header (' location:http://www.g.cn ');
    7. Header (' location:http://www.baidu.com ');
    8. Skip to Google
Copy Code
  • 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.