Discuz教程网

PHP header函数分析详解

[复制链接]
authicon dly 发表于 2011-9-3 19:03:15 | 显示全部楼层 |阅读模式
在php语言中,header()这个函数很有用的,尤其在用到ajax时候,他会帮你解决一些意想不到的问题。下面是header的一些详细讲解。希望对phper有帮助
代码如下:

  1. <?php
  2. // fix 404 pages:
  3. header('HTTP/1.1 200 OK');
  4. // set 404 header:
  5. header('HTTP/1.1 404 Not Found');
  6. // set Moved Permanently header (good for redrictions)
  7. // use with location header
  8. header('HTTP/1.1 301 Moved Permanently');
  9. // redirect to a new location:
  10. header('Location: http://www.example.org/');
  11. // redrict with delay:
  12. header('Refresh: 10; url=http://www.example.org/');
  13. print 'You will be redirected in 10 seconds';
  14. // you could also use the HTML syntax:// <meta http-equiv="refresh" content="10;http://www.example.org/ />
  15. // override X-Powered-By: PHP:
  16. header('X-Powered-By: PHP/4.4.0');
  17. header('X-Powered-By: Brain/0.6b');
  18. // content language (en = English)
  19. header('Content-language: en');
  20. // last modified (good for caching)
  21. $time = time() – 60; // or filemtime($fn), etc
  22. header('Last-Modified: '.gmdate('D, d M Y H:i:s', $time).' GMT');
  23. // header for telling the browser that the content
  24. // did not get changed
  25. header('HTTP/1.1 304 Not Modified');
  26. // set content length (good for caching):
  27. header('Content-Length: 1234');
  28. // Headers for an download:
  29. header('Content-Type: application/octet-stream');
  30. header('Content-Disposition: attachment; filename="example.zip"');
  31. header('Content-Transfer-Encoding: binary');
  32. // load the file to send:readfile('example.zip');
  33. // Disable caching of the current document:
  34. header('Cache-Control: no-cache, no-store, max-age=0, must-revalidate');
  35. header('Expires: Mon, 26 Jul 1997 05:00:00 GMT');
  36. // Date in the pastheader('Pragma: no-cache');
  37. // set content type:
  38. header('Content-Type: text/html; charset=iso-8859-1');
  39. header('Content-Type: text/html; charset=utf-8');
  40. header('Content-Type: text/plain');
  41. // plain text file
  42. header('Content-Type: image/jpeg');
  43. // JPG picture
  44. header('Content-Type: application/zip');
  45. // ZIP file
  46. header('Content-Type: application/pdf');
  47. // PDF file
  48. header('Content-Type: audio/mpeg');
  49. // Audio MPEG (MP3,…) file
  50. header('Content-Type: application/x-shockwave-flash');
  51. // Flash animation// show sign in box
  52. header('HTTP/1.1 401 Unauthorized');
  53. header('WWW-Authenticate: Basic realm="Top Secret"');
  54. print 'Text that will be displayed if the user hits cancel or ';
  55. print 'enters wrong login data';
  56. ?>
复制代码









上一篇:PHP缓存技术的使用说明
下一篇:PHP代码运行时间查看类代码分享
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

1314学习网 ( 浙ICP备10214163号 )

GMT+8, 2025-5-2 21:51

Powered by Discuz! X3.4

© 2001-2013 Comsenz Inc.

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