- Header ("location:http://bbs.it-home.org";);
- Exit After each redirect, "Exit" must be added to avoid an error and continue execution.
- ?>
- Header ("refresh:3;url=http://bbs.it-home.org");
- Print (' Loading, please wait ...
Three seconds after the automatic jump ~ ~ ~ ');
- Header redirection is equivalent to entering a URL for the user in the Address bar
- ?>
Copy CodeExample 2, prevent pages from being cached in IE
- Header (' Expires:mon, Jul 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-ridate ');
- Header (' cache-control:post-check=0, pre-check=0 ', false);
- Header (' Pragma:no-cache ');//compatible with http1.0 and HTTPS
- ?>
- CacheControl = No-cache
- Pragma=no-cache
- Expires =-1
Copy CodeNote: If the Web page on the server changes frequently, set the expires to 1, which means that it expires immediately. If a webpage is updated daily 1 o'clock in the morning, you can set expires to 1 o'clock in the morning the next day. When the HTTP1.1 server specifies CacheControl = No-cache, the browser does not cache the page. Legacy HTTP 1.0 Servers cannot use the Cache-control header. So for backwards compatibility with HTTP 1.0 servers, IE uses the Pragma:no-cache header to provide special support for HTTP. Internet Explorer does not cache this response if the client communicates with the server over a secure connection (https://) and the server returns a Pragma:no-cache header in the response. Note: Pragma:no-cache prevents caching only when used in a secure connection, and if used in a non-secure page, is processed in the same way as expires:-1, the page is cached but is marked for immediate expiration. HTTP-EQUIV META tag: You can use HTTP-EQUIV meta to mark the header of a specified HTTP message in an HTML page. Older versions of IE may not support HTML meta tags, so it is best to use the HTTP message header to disable caching. Example 3, let the user's browser appear unable to find the file information. Online data show: Php function header () can send a status header to the browser, for example:
- Header ("status:404 not Found")
Copy Code。 In fact, the response returned by the browser is:
- Header ("http/1.1 404 Not Found");
Copy CodeThe first part is the version of the HTTP Protocol (http-version), the second part is the status code, and the third part is the reason phrase (reason-phrase). Example 4, let the user download the file (the location of hidden files) HTML tags can be used to achieve normal file download. If in order to keep confidential files, you can not send the file link to others, you may use the header function for file download.
- Header ("Content-type:application/x-gzip");
- Header ("content-disposition:attachment; Filename= file name ");
- Header ("Content-description:php3 Generated Data");
- ?>
Copy CodeExample 5, the header function before the input content in general before the header function can not output HTML content, similar to the Setcookie () and the session function, these functions need to increase the message header information in the output stream. If there is an echo statement before the header () is executed, the "Warning:cannot Modify header Information–headers already sent by ..." error is reported when the header () is encountered later. There can be no 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 error notation, there is a blank line between the two PHP code snippets:
- Some code here
- ?>
- This is supposed to be a blank line.
- Header ("http/1.1 403 Forbidden");
- Exit ();
- ?>
Copy CodeCause analysis: 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 immediately sent, instead it is saved to a list. This allows you to modify the header information, including the default title (such as the Content-type title). However, once the script sends 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, and will send one of the above error messages. Workaround: Modify php.ini to open the cache (output_buffering), or use the cache function Ob_start (), Ob_end_flush (), etc. in the program. Principle Analysis: When output_buffering is enabled, PHP does not send HTTP headers when the script sends output. Instead, it imports this output through a pipeline (pipe) into a dynamically increasing cache (only used in PHP 4.0, which has a centralized output mechanism). You can either modify/Add the header or set the 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 contents of the output buffer. Attached, some other examples of PHP header functions.
- 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 301 Moved permanently ');
- Go to a new address
- Header (' location:http://bbs.it-home.org/');
- File Delay Steering:
- Header (' refresh:10; url=http://bbs.it-home.org/');
- print ' You'll be redirected in ten seconds ';
- Of course, you can also use HTML syntax to implement
- //
- Override x-powered-by:php:
- Header (' x-powered-by:php/4.4.0′ ');
- Header (' x-powered-by:brain/0.6b ');
- Document language
- Header (' content-language:en ');
- Tell the browser when it was last modified
- $time = time () –60; or Filemtime ($FN), etc
- Header (' last-modified: '. Gmdate (' d, D M Y h:i:s ', $time). ' GMT ');
- Tell the browser that the contents of the document have not changed
- Header (' http/1.1 304 not Modified ');
- Set Content length
- Header (' content-length:1234′ ');
- Set as a download type
- Header (' Content-type:application/octet-stream ');
- Header (' content-disposition:attachment; filename= ' Example.zip ');
- Header (' content-transfer-encoding:binary ');
- Load the file to send:
- ReadFile (' Example.zip ');
- Disable caching for the current document
- Header (' Cache-control:no-cache, No-store, max-age=0, Must-ridate ');
- Header (' Expires:mon, Jul 1997 05:00:00 GMT '); Date in the past
- Header (' Pragma:no-cache ');
- To set the 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 pictures
- Header (' Content-type:application/zip '); ZIP file
- Header (' content-type:application/pdf '); PDF file
- Header (' Content-type:audio/mpeg '); Audio files
- Header (' Content-type:application/x-shockwave-flash '); Flash Animation
- Show Login dialog box
- Header (' http/1.1 401 Unauthorized ');
- Header (' Www-authenticate:basic realm= ' Top Secret ');
- print ' Text that would be displayed if the user hits cancel or ';
- print ' enters wrong login data ';
- ?>
Copy Code |