- Header ("Location: http://bbs.it-home.org ";);
- Exit; // after each redirection, you must add "exit" to avoid errors and continue execution.
- ?>
- Header ("refresh: 3; url = http://bbs.it-home.org ");
- Print ('loading... please wait...
Automatic jump in 3 seconds ~~~ ');
- Header redirection is equivalent to inputting a url in the address bar for the user.
- ?>
Example 2: Disable page caching in IE
- Header ('expires: Mon, 26 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
Note: If the webpage on the server changes frequently, set Expires to-1, indicating that the page will expire immediately. If a webpage is updated at every day, you can set Expires to the next day. When CacheControl = no-cache is specified on the HTTP1.1 server, the browser does not cache the webpage. The Cache-Control title cannot be used for legacy HTTP 1.0 servers. To be backward compatible with HTTP 1.0 servers, IE uses the Pragma: no-cache title to provide special support for HTTP. If the client communicates with the server through a secure connection (https: //) and the server returns the Pragma: no-cache title in the response, Internet Explorer does not cache the response.
Note: Pragma: no-cache prevents caching only when used in secure connections. if used on a non-secure page, the processing method is the same as Expires:-1, this page will be cached but marked as expired immediately.
Http-equiv meta tag: you can use http-equiv meta to tag the specified http message header on the html page. Earlier versions of IE may not support html meta tags, so it is best to disable caching using the http message header.
Example 3: make the user's browser unable to find the file information.
The information on the Internet shows that the php function header () can send the Status header to the browser, for example:
- Header ("Status: 404 Not Found ")
. In fact, the response returned by the browser is:
- Header ("http/1.1 404 Not Found ");
The first part is the HTTP Version, the second part is the Status code, and the third part is the Reason Phrase (Reason-Phrase ).
Example 4: users can download normal files by using html tags to download files (hide the location of files. To keep files confidential, you can use the header function to download files.
- Header ("Content-type: application/x-gzip ");
- Header ("Content-Disposition: attachment; filename = file name ");
- Header ("Content-Description: PHP3 Generated Data ");
- ?>
Example 5: The input content before the header function generally cannot output html content before the header function. for example, there are setcookie () and session functions. these functions need to add message header information to the output stream.
If an echo statement exists before the header () is executed, when header () is encountered later, "Warning: Cannot modify header information-headers already sent by…" is reported ...." Error. There cannot be any text, blank lines, and carriage return before these functions, and it is best to add the exit () function after the header () function.
For example, the following error code contains an empty line between two php code segments:
- // Some code here
- ?>
- // It should be a blank line
- Header ("http/1.1 403 Forbidden ");
- Exit ();
- ?>
Cause Analysis: When the PHP script starts to be executed, it can simultaneously send the http message header (title) information and subject information. The http message header (from header () or SetCookie () function) is not sent immediately. Instead, it is saved to a list.
In this way, you can modify the title information, including the default title (for example, Content-Type title ).
However, once the script sends any non-title output (for example, called using HTML or print (), PHP must first send all headers and then terminate the HTTP Header. Then, send the subject Data. From this moment 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 open the cache (output_buffering), or use the cache function ob_start () and ob_end_flush () in the program.
Principle Analysis: When output_buffering is enabled, PHP does not send HTTP headers when the script sends the output.
Instead, it inputs this output to the dynamically added cache through the pipeline (pipe) (which can only be used in PHP 4.0 and has a centralized output mechanism ).
You can modify/add the header or set the cookie because the header is not actually sent. When all scripts are terminated, PHP automatically sends the HTTP header to the browser, and then sends the content in the output buffer.
Appendix. Examples of other php header functions.
- // OK
- Header ('http/1.1 200 OK ');
- // Set a 404 header:
- Header ('http/1.1 404 Not Found ');
- // Set the 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 redirection:
- Header ('refresh: 10; url = http://bbs.it-home.org /');
- Print 'You will be redirected in 10 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 the last modification time
- $ 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 document content has not changed
- Header ('http/1.1 304 Not modified ');
- // Set the content length
- Header ('content-Length: 1234 ′);
- // Set it to 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, 26 Jul 1997 05:00:00 GMT'); // Date in the past
- Header ('pragma: no-cache ');
- // 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 image
- Header ('content-Type: application/zip'); // zip file
- Header ('content-Type: application/pdf '); // pdf file
- Header ('content-Type: audio/mpeg '); // audio File
- Header ('content-Type: application/x-shockwave-Flash'); // flash animation
- // Display the login dialog box
- Header ('http/1.1 401 unauthorized ');
- Header ('www-Authenticate: Basic realm = "Top Secret "');
- Print 'text that will be displayed if the user hits cancel or ';
- Print 'enters wrong login data ';
- ?>
|