PHP header函数用法举例

WBOY
Freigeben: 2016-07-25 08:57:45
Original
1034 Leute haben es durchsucht
  1. Header(“Location: http://bbs.it-home.org”;);
  2. exit; //在每个重定向之后都必须加上“exit”,避免发生错误后,继续执行。
  3. ?>
  4. header(“refresh:3;url=http://bbs.it-home.org”);
  5. print(‘正在加载,请稍等…
    三秒后自动跳转~~~’);
  6. header重定向 就等价于替用户在地址栏输入url
  7. ?>
复制代码

例2,禁止页面在IE中缓存

  1. header('Expires: Mon, 26 Jul 1997 05:00:00 GMT');
  2. header('Last-Modified: '.gmdate('D, d M Y H:i:s') .' GMT');
  3. header('Cache-Control: no-store, no-cache, must-ridate');
  4. header('Cache-Control: post-check=0, pre-check=0',false);
  5. header('Pragma: no-cache');//兼容http1.0和https
  6. ?>
  7. CacheControl = no-cache
  8. Pragma=no-cache
  9. Expires = -1
复制代码

说明: 如果服务器上的网页经常变化,就把Expires设置为-1,表示立即过期。 如果一个网页每天凌晨1点更新,可以把Expires设置为第二天的凌晨1点。 当HTTP1.1服务器指定CacheControl = no-cache时,浏览器就不会缓存该网页。 旧式 HTTP 1.0 服务器不能使用 Cache-Control 标题。所以为了向后兼容 HTTP 1.0 服务器,IE使用Pragma:no-cache 标题对 HTTP 提供特殊支持。 如果客户端通过安全连接 (https://) 与服务器通讯,且服务器在响应中返回 Pragma:no-cache 标题,则 Internet Explorer 不会缓存此响应。

注意: Pragma:no-cache 仅当在安全连接中使用时才防止缓存,如果在非安全页中使用,处理方式与 Expires:-1 相同,该页将被缓存,但被标记为立即过期。

http-equiv meta标记: 在html页面中可以用http-equiv meta来标记指定的http消息头部。 老版本的IE可能不支持html meta标记,所以最好使用http消息头部来禁用缓存。

例3,让使用者的浏览器出现找不到档案的信息。

网上资料显示:php的函数header()可以向浏览器发送Status标头, 例如:

  1. header(”Status: 404 Not Found”)
复制代码

。 实际上浏览器返回的响应却是:

  1. header(”http/1.1 404 Not Found”);
复制代码

第一部分为HTTP协议的版本(HTTP-Version); 第二部分为状态代码(Status); 第三部分为原因短语(Reason-Phrase)。

例4,让使用者下载档案( 隐藏文件的位置 ) html标签 就可以实现普通文件下载。 如果为了保密文件,就不能把文件链接告诉别人,可以用header函数实现文件下载。

  1. header(“Content-type: application/x-gzip”);
  2. header(“Content-Disposition: attachment; filename=文件名”);
  3. header(“Content-Description: PHP3 Generated Data”);
  4. ?>
复制代码

例5 ,header函数前输入内容 一般来说在header函数前不能输出html内容,类似的还有setcookie() 和 session 函数,这些函数需要在输出流中增加消息头部信息。

如果在header()执行之前有echo等语句,当后面遇到header()时,就会报出 “Warning: Cannot modify header information – headers already sent by ….”错误。 在这些函数的前面不能有任何文字、空行、回车等,而且最好在header()函数后加上exit()函数。

例如,以下的错误写法,在两个 php代码段之间有一个空行:

  1. //some code here
  2. ?>
  3. //这里应该是一个空行
  4. header(”http/1.1 403 Forbidden”);
  5. exit();
  6. ?>
复制代码

原因分析: PHP脚本开始执行 时,它可以同时发送http消息头部(标题)信息和主体信息。 http消息头部(来自 header() 或 SetCookie() 函数)并不会立即发送,相反,它被保存到一个列表中。

如此,即可允许修改标题信息,包括缺省的标题(例如 Content-Type 标题)。

但是,一旦脚本发送了任何非标题的输出(例如,使用 HTML 或 print() 调用),那么PHP就必须先发送完所有的Header,然后终止 HTTP header。 而后继续发送主体数据,从这时开始,任何添加或修改Header信息的试图都是不允许的,并会发送上述的错误消息之一。

解决办法: 修改php.ini打开缓存(output_buffering),或者在程序中使用缓存函数ob_start(),ob_end_flush() 等。

原理分析: output_buffering被启用时,在脚本发送输出时,PHP并不发送HTTP header。

相反,它将此输出通过管道(pipe)输入到动态增加的缓存中(只能在PHP 4.0中使用,它具有中央化的输出机制)。

可以修改/添加header,或者设置cookie,因为header实际上并没有发送。 当全部脚本终止时,PHP将自动发送HTTP header到浏览器,然后再发送输出缓冲中的内容。

附,其它一些有关php header函数的例子。

  1. // ok
  2. header(‘HTTP/1.1 200 OK’);
  3. //设置一个404头:
  4. header(‘HTTP/1.1 404 Not Found’);
  5. //设置地址被永久的重定向
  6. header(‘HTTP/1.1 301 Moved Permanently’);
  7. //转到一个新地址
  8. header(‘Location: http://bbs.it-home.org/’);
  9. //文件延迟转向:
  10. header(‘Refresh: 10; url=http://bbs.it-home.org/’);
  11. print ‘You will be redirected in 10 seconds’;
  12. //当然,也可以使用html语法实现
  13. //
  14. // override X-Powered-By: PHP:
  15. header(‘X-Powered-By: PHP/4.4.0′);
  16. header(‘X-Powered-By: Brain/0.6b’);
  17. //文档语言
  18. header(‘Content-language: en’);
  19. //告诉浏览器最后一次修改时间
  20. $time = time() – 60; // or filemtime($fn), etc
  21. header(‘Last-Modified: ‘.gmdate(‘D, d M Y H:i:s’, $time).’ GMT’);
  22. //告诉浏览器文档内容没有发生改变
  23. header(‘HTTP/1.1 304 Not Modified’);
  24. //设置内容长度
  25. header(‘Content-Length: 1234′);
  26. //设置为一个下载类型
  27. header(‘Content-Type: application/octet-stream’);
  28. header(‘Content-Disposition: attachment; filename=”example.zip”‘);
  29. header(‘Content-Transfer-Encoding: binary’);
  30. // load the file to send:
  31. readfile(‘example.zip’);
  32. // 对当前文档禁用缓存
  33. header(‘Cache-Control: no-cache, no-store, max-age=0, must-ridate’);
  34. header(‘Expires: Mon, 26 Jul 1997 05:00:00 GMT’); // Date in the past
  35. header(‘Pragma: no-cache’);
  36. //设置内容类型:
  37. header(‘Content-Type: text/html; charset=iso-8859-1′);
  38. header(‘Content-Type: text/html; charset=utf-8′);
  39. header(‘Content-Type: text/plain’); //纯文本格式
  40. header(‘Content-Type: image/jpeg’); //JPG图片
  41. header(‘Content-Type: application/zip’); // ZIP文件
  42. header(‘Content-Type: application/pdf’); // PDF文件
  43. header(‘Content-Type: audio/mpeg’); // 音频文件
  44. header(‘Content-Type: application/x-shockwave-flash’); //Flash动画
  45. //显示登陆对话框
  46. header(‘HTTP/1.1 401 Unauthorized’);
  47. header(‘WWW-Authenticate: Basic realm=”Top Secret”‘);
  48. print ‘Text that will be displayed if the user hits cancel or ‘;
  49. print ‘enters wrong login data’;
  50. ?>
复制代码


Verwandte Etiketten:
Quelle:php.cn
Erklärung dieser Website
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn
Beliebte Tutorials
Mehr>
Neueste Downloads
Mehr>
Web-Effekte
Quellcode der Website
Website-Materialien
Frontend-Vorlage
Über uns Haftungsausschluss Sitemap
Chinesische PHP-Website:Online-PHP-Schulung für das Gemeinwohl,Helfen Sie PHP-Lernenden, sich schnell weiterzuentwickeln!