The header function sends some header information in PHP. We can use it directly to do 301 jumps, etc. Below I will summarize the usage of the header function and some common questions. Solution.
Send a raw HTTP header [Http Header] to the client. The header is a string sent by the server before transmitting HTML data to the browser using HTTP protocol. A blank line is required between the header and the HTML file.
1. Direction.
Header("Location: http://www.mobiletrain.org");
exit; //"exit" must be added after each redirection to avoid continuing execution after an error occurs.
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-revalidate' ); header( 'Cache-Control: post-check=0, pre-check=0', false ); header( 'Pragma: no-cache' ); //兼容http1.0和https
3. Implement file download
header('Content-Type: application/octet-stream');//设置内容类型 header('Content-Disposition: attachment; filename="example.zip"'); //设置MIME用户作为附件下载 如果将attachment换成inline意思为在线打开 header('Content-Transfer-Encoding: binary');//设置传输方式 header('Content-Length: '.filesize('example.zip'));//设置内容长度 readfile('example.zip');//读取需要下载的文件
4. Send the Status header
header(”Status: 404 Not Found”);
to the browser, but I found that the response actually returned by the browser is:
// ok header(‘HTTP/1.1 200 OK’); //设置一个404头: header(‘HTTP/1.1 404 Not Found’); //设置地址被永久的重定向 header(‘HTTP/1.1 301 Moved Permanently’); HTTP/1.x 200 OK Date: Thu, 03 Aug 2006 07:49:11 GMT Server: Apache/2.0.55 (Win32)php/5.0.5 X-Powered-By: PHP/5.0.5 Status: 404 Not Found Content-Length: 0 Keep-Alive: timeout=15, max=98 Connection: Keep-Alive Content-Type: text/html 注意事项有以下几点.
There cannot be a space between Location and ":", otherwise it will An error occurred (note: I just tested it. In my local environment, there was no page jump, but no error was reported, I don’t know the reason);
There cannot be any output before using the header (note: Everyone knows this. If there is any output before the header, including blanks, the error "header already sent by xxx" will appear);
Things after the header will still be executed.
Recommended: "PHP Tutorial"
The above is the detailed content of What does php header mean?. For more information, please follow other related articles on the PHP Chinese website!