好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

PHP header() 函数使用方法总结 - php函数

PHP header() 函数使用方法总结

header() 函数向客户端发送原始的 HTTP 报头,主要包括有HTTP协议的版本、状态代码、原因短语等我们常用于跳转页面,状态发送与文件下载,下面我们一起来看看.

header分为三部分:

第一部分为HTTP协议的版本(HTTP-Version);

第二部分为状态代码(Status);

第三部分为原因短语(Reason-Phrase);

header()函数使用说明:

一、作用:

PHP只是以HTTP协议将HTML文档的标头送到浏览器,告诉浏览器具体怎么处理这个页面,至于传送的内容则需要熟悉一下HTTP协议了,与PHP无关了,可参照.http://www.w3.org/Protocols/rfc2616/rfc2616.

传统的标头一定包含下面三种标头之一,并只能出现一次.

Location:  xxxx:yyyy/zzzz     Content-Type:  xxxx/yyyy     Status:  nnn  xxxxxx   

二、先来了解一下HTTP协议的运作方式

HTTP协议是基于请求/响应范式的,一个客户机与服务器建立连接后,发送一个请求给服务器,请求方式的格式为,统一资源标识符、协议版本号,后边是MIME信息包括请求修饰符、客户机信息和可能的内容,服务器接到请求后,给予相应的响应信息,其格式为一个状态行包括信息的协议版本号、一个成功或错误的代码,后边是MIME信息包括服务器信息、实体信息和可能的内容.

它分四个过程,在HTTP协议中,服务端是指提供HTTP服务的部分,客户端是指你使用的浏览器或者下载工具等等。在通讯时,由客户端发出请求连接,服务端建立连接,然后,客户端发出HTTP请求(Request),服务端返回响应信息(Respond),由此完成一个HTTP操作.

三、HTTP协议状态码表示的意思

1××  保留

2××  表示请求成功地接收

3××  为完成请求客户需进一步细化请求

4××  客户错误

5××  服务器错误

例,代码如下:

// fix 404 pages: 用这个header指令来解决URL重写产生的404 header   header(‘HTTP/1.1 200 OK’);    // set 404 header: 页面没找到   header(‘HTTP/1.1 404 Not Found’);    // 页面被永久删除,可以告诉seo/seo.html" target="_blank">搜索引擎更新它们的urls   // set Moved Permanently header (good for redrictions)   // use with location header   header(‘HTTP/1.1 301 Moved Permanently’);  // 访问受限   header(‘HTTP/1.1 403 Forbidden’);  // 服务器错误   header(‘HTTP/1.1 500 Internal Server Error’);    // 重定向到一个新的位置   // redirect to a new location:   header(‘Location: http: //www.m-bang.com);     延迟一段时间后重定向  // redrict with delay:   header(‘Refresh: 10; url=http: //www.sina.com.cn’);   print ‘You will be redirected in 10 seconds’;    // 覆盖 X-Powered-By value   // override X-Powered-By: PHP:   header(‘X-Powered-By: PHP/4.4.0′);  header(‘X-Powered-By: Brain/0.6b’);    // 内容语言 (en = English)   // content language (en = English)   header(‘Content-language: en’);    //最后修改时间 (在缓存的时候可以用到)   // last modified (good for caching)   $time  = time() – 60;  // or filemtime($fn), etc   header(‘Last-Modified: ‘. gmdate (‘D, d M Y H:i:s’,  $time ).’ GMT’);    // 告诉浏览器要获取的内容还没有更新   // header for telling the browser that the content   // did not get changed   header(‘HTTP/1.1 304 Not Modified’);    // 设置内容的长度 (缓存的时候可以用到):   // set content length (good for caching):   header(‘Content-Length: 1234′);    // 用来下载文件:   // Headers for an download:   header(‘Content-Type: application/octet-stream’);  header(‘Content-Disposition: attachment; filename=]example.zip]‘);  header(‘Content-Transfer-Encoding: binary’);    // 禁止缓存当前文档:   // load the file to send:readfile(‘example.zip’);   // Disable caching of the current document:   header(‘Cache-Control: no-cache, no-store, max-age=0, must-revalidate’);  header(‘Expires: Mon, 26 Jul 1997 05:00:00 GMT’);  // 设置内容类型:   // Date in the pastheader(‘Pragma: no-cache’);   // set content type:   header(‘Content-Type: text/html; charset=iso-8859-1′);  header(‘Content-Type: text/html; charset=utf-8′);  header(‘Content-Type: text/plain’);    // plain text file   header(‘Content-Type: image/jpeg’);    // JPG picture   header(‘Content-Type: application/zip’);    // ZIP file   header(‘Content-Type: application/pdf’);    // PDF file   header(‘Content-Type: audio/mpeg’);    // Audio MPEG (MP3,…) file   header(‘Content-Type: application/x-shockwave-flash’);  //开源代码phpfensi.com   // 显示登录对话框,可以用来进行HTTP认证   // Flash animation// show sign in box   header(‘HTTP/1.1 401 Unauthorized’);  header(‘WWW-Authenticate: Basic realm=]Top Secret]‘);  print ‘Text that will be displayed  if  the user hits cancel  or  ‘;  print ‘enters wrong login da  ta’; 

现在表单的填写,我们可以用AJAX对用户随时进行验证,进行友好的提示,但是在用户没有留意AJAX友好提示,提交了错误的表单,跳回原页,而填写的信息却全部丢失了,要支持页面回跳,有以下的办法:

1.使用session_cache_limiter方法:session_cache_limiter(‘private,must-revalidate’);但是要值得注意的是 session_cache_limiter()方法要写在session_start()方法之前才有用;

2.用header来设置控制缓存的方法: header(‘Cache-control:private,must-revalidate’);

页面跳转要注意的几个问题总结:

1、location和[:]号间不能有空格,否则会出错.

2、在用header前不能有任何的输出.

3、header后的PHP代码还会被执行.

下面是和asp中重定向response.redirect的比较:

例1,代码如下:

response.redirect "test.asp" 

header("location:test.php"); 

两者区别:

asp的redirect函数可以在向客户发送头文件后起作用,如代码如下:

<html><head></head><body>   <%response.redirect  "test.asp" %>   </body></html>   //查是php中下例代码会报错:    <html><head></head><body>   <?   header( "location:test.php" );   ?>   </body></html>   //只能这样:    <?   header( "location:test.php" );   ?>  <html><head></head><body>...</body></html>  

即header函数之前不能向客户发送任何数据.

例2,asp中,代码如下:

<html><head></head><body>   <%   response.redirect  "a.asp"    response.redirect  "b.asp"    %>   </body></html>   //结果是重定向a.asp文件.    //php呢?    <?   header( "location:a.php" );   header( "location:b.php" );   ?>   <html><head></head><body></body></html>  

我们发现它重定向b.php.

原来在asp中执行redirect后不会再执行后面的代码.而php在执行header后,继续执行下面的代码.在这方面上php中的header重定向不如asp中的重定向.有时我们要重定向后,不能执行后面的代码:一般地我们用如下代码:

if (...)   header( "..." );   else    {   ...   } 

但是我们可以简单的用下面的方法:

if (...)   { header( "..." ); exit ();}  

还要注意的是,如果是用Unicode(UTF-8)编码时也会出现问题,需要调整缓存设置,代码如下:

<[email=%@]%@LANGUAGE= "VBSCRIPT[/email]"  CODEPAGE= "936" %>   <% if  Request.ServerVariables( "SERVER_NAME" )= "s.111cn.net"  then   response.redirect  "news/index.htm"    else %>   <%end  if %>   <script>   var  url = location.href;   if (url.indexOf( 'http://www.phpfensi.com/' )!=-1)location.href= '/index/index.htm' ;   if (url.indexOf( 'http://www.zhutiy.com/' )!=-1)location.href= '/index1/index.htm' ;   if (url.indexOf( 'http://www.phpfensi.com/' )!=-1)location.href= '/cn/index.asp' ;   if (url.indexOf( 'http://www.baidu.com/' )!=-1)location.href= '/cn/index.asp' ;   </script>

查看更多关于PHP header() 函数使用方法总结 - php函数的详细内容...

  阅读:50次