- Header ("http/1.1 404 Not Found");
Copy CodeFor the ErrorDocument 404/404.php function in Apache configuration, the
- Error_page 404/404.php;
Copy CodeChange into
- Error_page 404 =/404.php;
Copy CodePHP Heager 301
- Header ("http/1.1 301 Moved permanently");
- Header ("Location: $url");
- ?>
Copy CodeRegardless of how many headers the page has, it executes the last one, but conditionally, for example:
- Header (' location:http://bbs.it-home.org ');
- Header (' location:http://www.g.cn ');
- Header (' location:http://www.baidu.com ');
Copy CodeThis will jump to Baidu
- Header (' location:http://bbs.it-home.org '); Echo ' Programmer's house;
- Header (' location:http://www.g.cn ');
- Header (' location:http://www.baidu.com ');
Copy CodeThis will jump to Google about the header function of the detailed use of the role: PHP is just the HTTP protocol to the HTML document header to the browser, tell the browser how to handle this page, as for the content of the transfer will need to familiarize yourself with the HTTP protocol, not related to PHP The traditional header must contain one of the following three headers and can only occur once.
- <1> redirection features, the most common
- Header ("location:http://bbs.it-home.org/");
- ?>
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.
- Tell the browser how long this page will expire (in Greenwich Mean Time), as long as the date has passed.
- Header ("Expires:mon, Jul 1970 05:00:00 GMT");
- 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
- Header ("last-modified:".) Gmdate ("D, D M Y h:i:s"). "GMT");
- Tell the client browser not to use the cache
- Header ("Cache-control:no-cache, must-revalidate");
- Parameters (compatible with previous servers), which is compatible with the HTTP1.0 protocol
- Header ("Pragma:no-cache");
- Output MIME type
- Header ("Content-type:application/file");
- File length
- Header ("content-length:227685");
- Accepted range units
- Header ("Accept-ranges:bytes");
- File name in the File Save dialog box by default
- Header ("content-disposition:attachment; Filename= $filename ");
- ?>
Copy Code<3> output status value to the browser, mainly for access rights control
- Header (' http/1.1 401 Unauthorized ');
- Header (' status:401 unauthorized ');
- ?>
Copy CodeFor 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
- Header (' http/1.1 404 Not Found ');
- Header ("status:404 not Found");
- ?>
Copy CodeNote: 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). Use example one: This example redirects the browser to the official PHP website.
- Header ("location:http://bbs.it-home.org/"); Exit
Copy CodeExample two: to the user each time can get the latest information, instead of Proxy or cache data, you can use the following headers
- 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-cache, must-revalidate");
- Header ("Pragma:no-cache");
Copy CodeExample three: Let the user's browser appear unable to find the file information.
- Header ("status:404 not Found");
Copy CodeExample four: Let the user download the file.
- Header ("Content-type:application/x-gzip");
- Header ("content-disposition:attachment; Filename= file name ");
- Header ("Content-description:php3 Generated Data");
Copy CodeHeader-Sends an original HTTP header stating void Header (string string [, bool replace [, int http_response_code]]) The header () function is used to send an original HTTP header. See the http/1.1 specification for more information about HTTP headers. The optional parameter, replace, indicates whether to replace the previous similar header or to add a header of the same type. The default is replace, but if you set it to FALSE, you can force multiple, homogeneous headers to be sent. For example:
- Header (' www-authenticate:negotiate ');
- Header (' WWW-AUTHENTICATE:NTLM ', false);
- ?>
Copy CodeThe Second optional parameter, Http_response_code, forces the HTTP response code to be set to the specified value (this parameter is a new addition to PHP 4.3.0). There are two special types of header calls. The first is that the header begins with the string "http/" (not important in case) and can be used to determine the HTTP status code to send. For example, if Apache is configured with PHP to handle error-handling requests that cannot find files (using the ErrorDocument Directive), you need to make sure that the script produces the correct status code.
- Header ("http/1.0 404 Not Found")
- ?>
Copy CodeNote: The HTTP status code header line is always the first to be sent to the client, and does not control whether the actual header () call is the first one. Unless the HTTP header has been sent, you can overwrite the original by invoking the header () function with the new state line at any time. HTTP status Detection (HTTP Header): The second special case is the "location:" header. It does not just send the label back to the browser, it also returns a REDIRECT (302) status code to the browser, unless a 3XX status code has been issued before.
- Header ("location:http://www.example.com/"); /* Redirect Browser */
- /* Ensure that subsequent code is not executed after redirection */
- Exit
- ?>
Copy CodeNote: The http/1.1 standard requires an absolute address URI as the location: parameter, but there are some clients that support relative URIs. You can usually use $_server[' http_host ', $_server[' php_self '] and the dirname () function to generate an absolute URI from a relative URI:
- Header ("location:http://%22.$_server[' http_host '/]
- . RTrim (dirname ($_server[' php_self '), '/\\ ')
- ." /". $relative _url);
- ?>
Copy CodeNote: Even if the session.use_trans_sid,session ID is enabled, it will not be passed along with the location header information. Must be passed manually with SID constants. PHP scripts often produce dynamic content that must not be cached by the browser or proxy server. Many proxy servers and browsers can be disabled by the following methods: Note: You may find that even if you do not output all of the above code, the Web page is not buffered. There are many options that users can set to change the browser's default caching behavior. By sending the above header, you should be able to overwrite any settings that could cause the script page to be cached. In addition, when a session is used, the Session_cache_limiter () function and the Session.cache_limiter option can be used to automatically generate the correct cache-related headers. Remember that the header () must be called before any actual output, whether it's from a normal HTML tag, a blank line, or PHP. One common mistake is that when reading code through include (), require (), or some other file access class function, some spaces or blank lines are sent before the header () is called. This error is also common in a separate php/html file.
- Header ("Cache-control:no-cache, must-revalidate"); http/1.1
- Header ("Expires:mon, Jul 1997 05:00:00 GMT"); The past time
- ?>
Copy Code
- /* This will produce an error because the header ()
- * I've been outputting something before * *
- Header (' location:http://www.example.com/');
- ?>
Copy CodeNote: Since PHP 4, some output buffering functions can be used to solve this problem. The price is to keep all the output from the browser on the server until the next command sends them. You can use Ob_start () and Ob_end_flush () in your code to implement such functionality, either by modifying the output_buffering configuration options in php.ini, or by modifying the server configuration file. Header () Two common uses:
- To set the page encoding:
- Header (' content-type:text/html;charset=gb2312 ');
- Adjustment page:
- Header (' location:http://www.baidu.com ');
Copy Code |