Usage of the PHPheader () function (incorrect settings such as 301 and 404)-php Tutorial

Source: Internet
Author: User
Usage of the PHPheader () function (incorrect settings such as 301 and 404)

  1. Header ("HTTP/1.1 404 Not Found ");

For the ErrorDocument 404/404. php function in apache configuration

  1. Error_page 404/404 .php;

Change

  1. Error_page 404 =/404.php;

Php hemanager 301

  1. Header ("HTTP/1.1 301 Moved Permanently ");
  2. Header ("Location: $ url ");
  3. ?>

No matter how many headers there are on the page, it executes the last one, but it is conditional. for example:

  1. Header ('Location: http://bbs.it-home.org ');
  2. Header ('Location: http://www.g.cn ');
  3. Header ('Location: http://www.baidu.com ');

This will jump to Baidu

  1. Header ('Location: http://bbs.it-home.org '); echo 'programmer's house;
  2. Header ('Location: http://www.g.cn ');
  3. Header ('Location: http://www.baidu.com ');

This will jump to google's detailed instructions on the use of the header function 1. Role: PHP only sends the HTML document header to the browser using the HTTP protocol, telling the browser how to deal with this page, as for the transmitted content, you need to familiarize yourself with the HTTP protocol. PHP is irrelevant. the traditional header must contain one of the following three headers and can only appear once.

  1. <1> redirection function, which is the most common
  2. Header ("Location: http://bbs.it-home.org /");
  3. ?>

<2> force users to obtain the latest information each time they access this page, instead of using the cache that exists on the client.

  1. // Tell the browser the expiration time of this page (expressed by Greenwich Mean Time), as long as it is a date that has passed.
  2. Header ("Expires: Mon, 26 Jul 1970 05:00:00 GMT ");
  3. // Tell the browser the last update date (expressed by Greenwich Mean Time) of this page, that is, the day, to force the browser to obtain 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. // Parameter (compatible with the previous Server), that is, compatible with 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. // Unit of acceptable range
  14. Header ("Accept-Ranges: bytes ");
  15. // The file name in the "Save saved files" dialog box
  16. Header ("Content-Disposition: attachment; filename = $ filename ");
  17. ?>

<3> outputs the status value to the browser, which is mainly used for access permission control.

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

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

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

Note: The traditional header must contain one of the following three headers and can only appear once. Content-Type: xxxx/yyyy Location: xxxx: yyyy/zzzz Status: nnn xxxxxx can appear more than twice in the new Multipart MIME Type.

Example 1: redirect the browser to the official PHP website.

  1. Header ("Location: http://bbs.it-home.org/"); exit;

Example 2: to obtain the latest data every time, instead of the data in the Proxy or cache, you can use the following header

  1. Header ("Expires: Mon, 26 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 ");

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

  1. Header ("Status: 404 Not Found ");

Example 4: ask the user to 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 ");

Header -- send an original HTTP header description void header (string [, bool replace [, int http_response_code])

The header () function is used to send an original HTTP header. For more information about HTTP headers, see the HTTP/1.1 standard. The optional parameter replace indicates whether to replace the previous similar header or add a same type of header. It is replaced by default, but if it is set to FALSE, multiple similar headers can be forcibly sent. For example:

  1. Header ('www-Authenticate: negotiate ');
  2. Header ('www-Authenticate: ntlm', false );
  3. ?>

The second optional parameter http_response_code sets the HTTP response code to a specified value (this parameter is newly added by PHP 4.3.0 ).

There are two special header calls. First, the header starts with the string "HTTP/" (case insensitive) and can be used to determine the HTTP status code to be sent. For example, if Apache is configured to use PHP to handle error handling requests that cannot find files (using the ErrorDocument command), make sure that the script generates a correct status code.

  1. Header ("HTTP/1.0 404 Not Found ")
  2. ?>

Note: the HTTP status code header line is always the first to be sent to the client, regardless of whether the actual header () call is the first. Unless the HTTP header has been sent out, you can use a new status line to call the header () function to overwrite the original one.

HTTP status detection (HTTP Header ):

The second special case is the "Location:" header. It not only sends this header back to the browser, but also returns a REDIRECT (302) status code to the browser, unless a 3xx status code has been issued before.

  1. Header ("Location: http://www.example.com/");/* redirect browser */
  2. /* Ensure that subsequent code will not be executed after redirection */
  3. Exit;
  4. ?>

Note: the HTTP/1.1 standard requires an absolute url uri as the Location: parameter, but some clients support relative URIs. You can use the $ _ SERVER ['http _ host'], $ _ SERVER ['php _ SELF '], and dirname () functions to generate an absolute URI from the relative URI:

  1. Header ("Location: http: // % 22. $ _ server ['http _ host'/]
  2. . Rtrim (dirname ($ _ SERVER ['php _ SELF ']),'/\ ')
  3. . "/". $ Relative_url );
  4. ?>

Note: Even if session. use_trans_sid is enabled, the Session ID will not be transmitted along with the Location header. The SID constant must be passed manually. PHP scripts usually generate some dynamic content, which must not be cached by browsers or proxies. Many proxy servers and browsers can be cached by the following method: Note: The webpage may not be buffered even if no code above is output. You can set many options to change the default cache behavior of your browser. By sending the above header, you can overwrite any settings that can cause the script page to be cached. In addition, when session is used, the session_cache_limiter () function and session. cache_limiter option can be used to automatically generate correct cache-related headers. Remember that header () must be called before any actual output, whether from common HTML tags, empty lines, or PHP. One common error is that when reading code through include (), require () or some other file access function, some spaces or blank lines call header () previously sent. This error is also common in a separate PHP/HTML file.

  1. Header ("Cache-Control: no-cache, must-revalidate"); // HTTP/1.1
  2. Header ("Expires: Mon, 26 Jul 1997 05:00:00 GMT"); // past time
  3. ?>

  1. /* This will generate an error because the header ()
  2. * Something has been output before */
  3. Header ('Location: http://www.example.com /');
  4. ?>

Note: From PHP 4, some output buffer functions can be used to solve this problem. The cost is to cache all the output to the browser on the server until the following command sends them. You can use ob_start () and ob_end_flush () in the code to implement this function, or modify php. the output_buffering configuration option in ini can also be implemented by modifying the server configuration file.

Appendix header:

  1. // Set the page encoding:
  2. Header ('content-Type: text/html; charset = gb2312 ');
  3. // Adjust the page:
  4. Header ('Location: http://www.baidu.com ');

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.