Home  >  Article  >  Backend Development  >  Detailed explanation of the usage of php header function (2)

Detailed explanation of the usage of php header function (2)

WBOY
WBOYOriginal
2016-07-25 08:56:151067browse
  1. Header("Location: http://www.php.net";);
  2. exit; //"exit" must be added after each redirect to avoid errors After that, continue execution.
  3. ?>
  4. header("refresh:3;url=http://bbs.it-home.org");
  5. print('Loading, please wait...
    Three seconds Automatically jump after~~~');
  6. Header redirection is equivalent to entering the url in the address bar for the user
  7. ?>
Copy the code

Example 2: Disable page caching in IE To allow users to get the latest data every time instead of the data in the proxy or cache, you can use the following headers

  1. header('Expires: Mon, 26 Jul 1997 05:00:00 GMT');
  2. header('Last-Modified: '.gmdate('D, d M Y H:i: s') .' GMT');
  3. header('Cache-Control: no-store, no-cache, must-ridate');
  4. header('Cache-Control: post-check=0, pre-check=0 ',false);
  5. header('Pragma: no-cache');//Compatible with http1.0 and https
  6. ?>
Copy code

CacheControl = no-cache Pragma=no-cache Expires = -1 Expires is a good thing. If the web pages on the server change frequently, set it to -1 to indicate immediate expiration. If a web page is updated at 1 am every day, you can set Expires to 1 am the next day. When the HTTP1.1 server specifies CacheControl = no-cache, the browser will not cache the web page.

Legacy HTTP 1.0 servers cannot use the Cache-Control header. So for backward compatibility with HTTP 1.0 servers, IE provides special support for HTTP using the Pragma:no-cache header. If the client communicates with the server over a secure connection (https://) and the server returns the Pragma:no-cache header in the response, Internet Explorer will not cache the response. Note: Pragma:no-cache only prevents caching when used in a secure connection. If used in a non-secure page, the handling is the same as Expires:-1. The page will be cached but marked as expired immediately.

http-equiv meta tag: You can use http-equiv meta to mark the specified http message header in the html page. Older versions of IE may not support html meta tags, so it's best to use http message headers to disable caching. Example 3: Let the user's browser display a file not found message. PHP's function header() can send the Status header to the browser. For example: header("Status: 404 Not Found"). But the actual response returned by the browser is:

  1. header(“Content-type: application/x-gzip”);
  2. header(“Content-Disposition: attachment; filename=filename”);
  3. header(“Content-Description : PHP3 Generated Data");
  4. ?>
Copy code

Example 5: Input content before header function Generally speaking, HTML content cannot be output before the header function. Similarly, there are setcookie() and session functions. These functions need to add message header information to the output stream. If there are statements such as echo before header() is executed, when header() is encountered later, a “Warning: Cannot modify header information – headers already sent by….” error will be reported. That is to say, there cannot be any text, blank lines, carriage returns, etc. in front of these functions, and it is best to add the exit() function after the header() function. For example, in the following incorrect writing, there is a blank line between the two PHP code snippets:

  1. //some code here
  2. ?>
  3. //This should be a blank line
  4. header("http/1.1 403 Forbidden");
  5. exit();
  6. ?>
Copy code

Reason: When a PHP script starts executing, it can send http message header (title) information and body information at the same time. The http message header (from the header() or 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 (such as the Content-Type header). However, once the script sends any non-header output (for example, using HTML or a print() call), then PHP You must first send all the headers, then terminate the HTTP header. Then continue to send the body data. From this point on, any attempt to add or modify the header information is not allowed, and one of the above error messages will be sent.

Solution: Modify php.ini to turn on caching (output_buffering), or use the caching functions ob_start(), ob_end_flush(), etc. in the program. The principle is: when output_buffering is enabled, PHP does not send HTTP headers when the script sends output. Instead, it pipes this output into a dynamically growing cache (only available in PHP 4.0, which has a centralized output mechanism). You can still modify/add headers, or set cookies, since headers are not actually sent. When all scripts terminate, PHP will automatically send HTTP headers to the browser, and then send the contents of the output buffer.



Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn