Discuz教程网

PHP header示例代码(推荐)

[复制链接]
authicon dly 发表于 2011-9-3 19:11:51 | 显示全部楼层 |阅读模式
代码如下:

  1. <?php
  2. /*** Function: PHP header() examples (PHP)
  3. ** Desc: Some examples on how to use the header() function of PHPYou find a detailed tutorial at expertsrt.com (English) or at ffm.junetz.de (German).These is also a good help about caching at web-caching.com.
  4. ** Example: see below. <br/><br/><b>Tip:</b> You can use these sites to check your headers: <a href="http://web-sniffer.net/">web-sniffer.net</a>, <a href="http://www.delorie.com/web/headers.html">delorie.com</a> or <a href="http://www.forret.com/projects/analyze/">www.forret.com</a>.
  5. ** Author: Jonas John
  6. */

  7. // fix 404 pages:
  8. header('HTTP/1.1 200 OK');

  9. // set 404 header:
  10. header('HTTP/1.1 404 Not Found');

  11. // set Moved Permanently header (good for redrictions)
  12. // use with location header
  13. header('HTTP/1.1 301 Moved Permanently');

  14. // redirect to a new location:
  15. header('Location: http://www.example.org/');

  16. // redrict with delay:
  17. header('Refresh: 10; url=http://www.example.org/');
  18. print 'You will be redirected in 10 seconds';

  19. // you could also use the HTML syntax:// <meta http-equiv="refresh" content="10;http://www.example.org/ />

  20. // override X-Powered-By: PHP:
  21. header('X-Powered-By: PHP/4.4.0');
  22. header('X-Powered-By: Brain/0.6b');

  23. // content language (en = English)
  24. header('Content-language: en');

  25. // last modified (good for caching)
  26. $time = time() – 60; // or filemtime($fn), etc
  27. header('Last-Modified: '.gmdate('D, d M Y H:i:s', $time).' GMT');

  28. // header for telling the browser that the content
  29. // did not get changed
  30. header('HTTP/1.1 304 Not Modified');

  31. // set content length (good for caching):
  32. header('Content-Length: 1234');

  33. // Headers for an download:
  34. header('Content-Type: application/octet-stream');
  35. header('Content-Disposition: attachment; filename="example.zip"');
  36. header('Content-Transfer-Encoding: binary');

  37. // load the file to send:readfile('example.zip');
  38. // Disable caching of the current document:
  39. header('Cache-Control: no-cache, no-store, max-age=0, must-revalidate');
  40. header('Expires: Mon, 26 Jul 1997 05:00:00 GMT');
  41. // Date in the pastheader('Pragma: no-cache');
  42. // set content type:
  43. header('Content-Type: text/html; charset=iso-8859-1');
  44. header('Content-Type: text/html; charset=utf-8');
  45. header('Content-Type: text/plain');

  46. // plain text file
  47. header('Content-Type: image/jpeg');

  48. // JPG picture
  49. header('Content-Type: application/zip');

  50. // ZIP file
  51. header('Content-Type: application/pdf');

  52. // PDF file
  53. header('Content-Type: audio/mpeg');

  54. // Audio MPEG (MP3,…) file
  55. header('Content-Type: application/x-shockwave-flash');

  56. // Flash animation// show sign in box
  57. header('HTTP/1.1 401 Unauthorized');
  58. header('WWW-Authenticate: Basic realm="Top Secret"');
  59. print 'Text that will be displayed if the user hits cancel or ';
  60. print 'enters wrong login data';
  61. ?>
复制代码







上一篇:PHP数据库连接时容易出错的特殊符号问题
下一篇:PHPnow安装服务[apache_pn]失败的问题的解决方法
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

1314学习网 ( 浙ICP备10214163号 )

GMT+8, 2025-5-3 01:18

Powered by Discuz! X3.4

© 2001-2013 Comsenz Inc.

快速回复 返回顶部 返回列表