Home >Backend Development >PHP Tutorial >Detailed explanation of php header function_PHP tutorial

Detailed explanation of php header function_PHP tutorial

WBOY
WBOYOriginal
2016-07-20 11:03:31856browse

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
 代码如下 复制代码

Header(“Location: http://www.bkjia.com”;);
exit; //在每个重定向之后都必须加上“exit”,避免发生错误后,继续执行。
?>

Header(“Location: http://www.bkjia.com”;);

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( ‘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
?>
CacheControl = no-cache
Pragma=no-cache
Expires = -1

Disable page caching in IE

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('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'));//设置内容长度
  // load the file to send:
readfile('example.zip');//读取需要下载的文件

header( ‘Cache-Control: no-store, no-cache, must-revalidate’ );

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
 代码如下 复制代码
header(”Status: 404 Not Found”)。
Expires = -1

 代码如下 复制代码

// 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

Enable file download
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
PHP’s function header() can send Status header , to the browser Such as But I found that the response actually returned by the browser is:
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;


www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/445284.htmlTechArticleThe header function sends some header information in PHP. If we can use it directly to make a 301 jump Wait, let me summarize the usage of header function and solutions to some common problems...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn