Header ()

Source: Internet
Author: User

This article mainly gives you a summary of PHP commonly used header head definition, very comprehensive and meticulous, the need for small partners can refer to. Original Address http://www.jb51.net/article/68159.htm

The header () function sends the original HTTP header to the client.

It is important to recognize that the header () function must be called before any actual output is sent (in PHP 4 and later, you can use output caching to resolve this issue):

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556 <?php header(‘HTTP/1.1 200 OK‘); // ok 正常访问 header(‘HTTP/1.1 404 Not Found‘); //通知浏览器 页面不存在 header(‘HTTP/1.1 301 Moved Permanently‘); //设置地址被永久的重定向 301 header(‘Location: http://www.ithhc.cn/‘); //跳转到一个新的地址 header(‘Refresh: 10; url=http://www.ithhc.cn/‘); //延迟转向 也就是隔几秒跳转 header(‘X-Powered-By: PHP/6.0.0‘); //修改 X-Powered-By信息 header(‘Content-language: en‘); //文档语言 header(‘Content-Length: 1234‘); //设置内容长度 header(‘Last-Modified: ‘.gmdate(‘D, d M Y H:i:s‘, $time).‘ GMT‘); //告诉浏览器最后一次修改时间 header(‘HTTP/1.1 304 Not Modified‘); //告诉浏览器文档内容没有发生改变   ###内容类型### header(‘Content-Type: text/html; charset=utf-8‘); //网页编码 header(‘Content-Type: text/plain‘); //纯文本格式 header(‘Content-Type: image/jpeg‘); //JPG、JPEG  header(‘Content-Type: application/zip‘); // ZIP文件 header(‘Content-Type: application/pdf‘); // PDF文件 header(‘Content-Type: audio/mpeg‘); // 音频文件  header(‘Content-type: text/css‘); //css文件 header(‘Content-type: text/javascript‘); //js文件 header(‘Content-type: application/json‘); //json header(‘Content-type: application/pdf‘); //pdf header(‘Content-type: text/xml‘); //xml header(‘Content-Type: application/x-shockw**e-flash‘); //Flash动画   ######   ###声明一个下载的文件### header(‘Content-Type: application/octet-stream‘); header(‘Content-Disposition: attachment; filename="ITblog.zip"‘); header(‘Content-Transfer-Encoding: binary‘); readfile(‘test.zip‘); ######   ###对当前文档禁用缓存### header(‘Cache-Control: no-cache, no-store, max-age=0, must-revalidate‘); header(‘Expires: Mon, 26 Jul 1997 05:00:00 GMT‘); ######   ###显示一个需要验证的登陆对话框###  header(‘HTTP/1.1 401 Unauthorized‘);  header(‘WWW-Authenticate: Basic realm="Top Secret"‘);  ######     ###声明一个需要下载的xls文件### header(‘Content-Disposition: attachment; filename=ithhc.xlsx‘); header(‘Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet‘); header(‘Content-Length: ‘.filesize(‘./test.xls‘));  header(‘Content-Transfer-Encoding: binary‘);  header(‘Cache-Control: must-revalidate‘);  header(‘Pragma: public‘);  readfile(‘./test.xls‘);  ###### ?>
PHP header () function usage instructions

Original Address http://www.jb51.net/article/15112.htm

PHP only in 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 needs to be familiar with the HTTP protocol, not related to PHP, you can refer to the header () function instructions:

first, the role:
~~~~~~~~~
PHP only in the HTTP protocol to the HTML document header to the browser, tell the browser how to deal with this page, as for the content of the transfer will need to familiarize yourself with the HTTP protocol, not related to PHP, can refer to http://www.w3.org/Protocols/rfc2616/rfc2616.
The traditional header must contain one of the following three headers and can only occur once.
Location:xxxx:yyyy/zzzz
Content-type:xxxx/yyyy
status:nnn xxxxxx

second, first to understand how the HTTP protocol works
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
The HTTP protocol is based on the request/response paradigm. After a client establishes a connection to the server, it sends a request to the server in the form of a Uniform Resource Identifier, protocol version number, followed by MIME information including the request modifier, client information, and possible content. After the server receives the request, it gives the corresponding response information in the form of a status line that includes the protocol version number of the information, a successful or incorrect code, followed by MIME information including server information, entity information, and possible content.
It is divided into four processes, in the HTTP protocol, the server is the part that provides the HTTP service, the client refers to the browser you use or download tools and so on. In the communication, the client makes a request connection, the server establishes the connection, and then the client makes an HTTP request (request) and the server returns the response information (Respond), thus completing an HTTP operation.

third, the meaning of the HTTP protocol status code representation
~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1XX reserved
2XX indicates that the request was successfully received by
3XX request further refinement of requests for completion of customer requests
4XX Customer Error
5XX Server Error

Four, the operation example:
~~~~~~~~~~~~~
<1> redirection features, the most common
Copy CodeThe code is as follows:
<?php
Header ("Location:http://www.php.net");
?>

<2> forces users to get the latest information each time they access the page, rather than using a cache that exists on the client.
Copy CodeThe code is as follows:
<?php
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 ");
?>

<3> output status value to the browser, mainly for access rights control
Copy CodeThe code is as follows:
<?php
Header (' http/1.1 401 Unauthorized ');
Header (' status:401 unauthorized ');
?>
For 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
Copy CodeThe code is as follows:
<?php
Header (' http/1.1 404 Not Found ');
Header ("status:404 not Found");
?>

Header ()

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.