当前位置:Gxlcms > PHP教程 > php使用header函数设置各种HTTP头的例子

php使用header函数设置各种HTTP头的例子

时间:2021-07-01 10:21:17 帮助过:29人阅读

  1. /**
  2. desc:header函数设置各种HTTP头
  3. link:bbs.it-home.org
  4. date:2013/2/24
  5. */
  6. //定义编码
  7. header( 'Content-Type:text/html;charset=utf-8 ');
  8. //Atom
  9. header('Content-type: application/atom+xml');
  10. //CSS
  11. header('Content-type: text/css');
  12. //Javascript
  13. header('Content-type: text/javascript');
  14. //JPEG Image
  15. header('Content-type: image/jpeg');
  16. //JSON
  17. header('Content-type: application/json');
  18. //PDF
  19. header('Content-type: application/pdf');
  20. //RSS
  21. header('Content-Type: application/rss+xml; charset=ISO-8859-1');
  22. //Text (Plain)
  23. header('Content-type: text/plain');
  24. //XML
  25. header('Content-type: text/xml');
  26. // ok
  27. header('HTTP/1.1 200 OK');
  28. //设置一个404头:
  29. header('HTTP/1.1 404 Not Found');
  30. //设置地址被永久的重定向
  31. header('HTTP/1.1 301 Moved Permanently');
  32. //转到一个新地址
  33. header('Location: http://bbs.it-home.org/');
  34. //文件延迟转向:
  35. header('Refresh: 10; url=http://bbs.it-home.org/');
  36. print 'You will be redirected in 10 seconds';
  37. //当然,也可以使用html语法实现
  38. // header('Content-Transfer-Encoding: binary');
  39. // load the file to send:
  40. readfile('example.zip');
  41. // 对当前文档禁用缓存
  42. header('Cache-Control: no-cache, no-store, max-age=0, must-revalidate');
  43. header('Expires: Mon, 26 Jul 1997 05:00:00 GMT'); // Date in the past
  44. header('Pragma: no-cache');
  45. //设置内容类型:
  46. header('Content-Type: text/html; charset=iso-8859-1');
  47. header('Content-Type: text/html; charset=utf-8');
  48. header('Content-Type: text/plain'); //纯文本格式
  49. header('Content-Type: image/jpeg'); //JPG***
  50. header('Content-Type: application/zip'); // ZIP文件
  51. header('Content-Type: application/pdf'); // PDF文件
  52. header('Content-Type: audio/mpeg'); // 音频文件
  53. header('Content-Type: application/x-shockw**e-flash'); //Flash动画
  54. //显示登陆对话框
  55. header('HTTP/1.1 401 Unauthorized');
  56. header('WWW-Authenticate: Basic realm="Top Secret"');
  57. print 'Text that will be displayed if the user hits cancel or ';
  58. print 'enters wrong login data';
  59. ?>

人气教程排行