Home >Backend Development >PHP Tutorial >Detailed explanation of php header function_PHP tutorial
The header function in PHP sends some header information. If we can use it directly to do 301 jumps, etc., let me summarize the usage of the header function and solutions to some common problems.
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 the HTTP protocol. A blank line is required between the header and the HTML file
Example 1
The code is as follows | Copy code | ||||
exit; // "exit" must be added after each redirection to avoid continuing execution after an error occurs. ?> |
代码如下 | 复制代码 |
header( ‘Expires: Mon, 26 Jul 1997 05:00:00 GMT’ ); |
header('Content-Type: application/octet-stream');//设置内容类型
header( ‘Cache-Control: post-check=0, pre-check=0′, false );
header( ‘Pragma: no-cache’ ); //Compatible with http1.0 and https
?>
The code is as follows
Copy code
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('Content-Disposition: attachment; filename="example.zip"'); //设置MIME用户作为附件下载 如果将attachment换成inline意思为在线打开
header('Content-Transfer-Encoding: binary');//设置传输方式
header('Content-Length: '.filesize('example.zip'));//设置内容长度
// load the file to send:
readfile('example.zip');//读取需要下载的文件
CacheControl = no-cache
Expires = -1
代码如下
复制代码
header(”Status: 404 Not Found”)。
代码如下 | 复制代码 |
// ok //设置一个404头: //设置地址被永久的重定向
|
The code is as follows | Copy code |
header('Content-Type: application/octet-stream');//Set the content type header('Content-Disposition: attachment; filename="example.zip"'); //Set the MIME user to download as an attachment. If attachment is replaced with inline, it means opening online header('Content-Transfer-Encoding: binary');//Set the transmission method header('Content-Length: '.filesize('example.zip'));//Set the content length // load the file to send: readfile('example.zip');//Read the file to be downloaded |
The code is as follows | Copy code |
// 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’); 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 |
The following points should be noted:
•There cannot be a space between Location and ":", otherwise an error will occur (Note: I just tested it. In my local environment, there is no page jump, but no error is 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;