php header函数使用实例代码

原创
2016-06-13 10:40:16 533浏览

  1. /*** Function: PHP header() examples (PHP)
  2. ** 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.
  3. ** Example: see below.

    Tip: You can use these sites to check your headers: web-sniffer.net, delorie.com or www.forret.com.
  4. ** Author: Jonas John
  5. */
  6. // fix 404 pages:
  7. header(HTTP/1.1 200 OK);
  8. // set 404 header:
  9. header(HTTP/1.1 404 Not Found);
  10. // set Moved Permanently header (good for redrictions)
  11. // use with location header
  12. header(HTTP/1.1 301 Moved Permanently);
  13. // redirect to a new location:
  14. header(Location: http://www.example.org/);
  15. // redrict with delay:
  16. header(Refresh: 10; url=http://www.example.org/);
  17. print You will be redirected in 10 seconds;
  18. // you could also use the HTML syntax://
  19. header(Content-Transfer-Encoding: binary);
  20. // load the file to send:readfile(example.zip);
  21. // Disable caching of the current document:
  22. header(Cache-Control: no-cache, no-store, max-age=0, must-revalidate);
  23. header(Expires: Mon, 26 Jul 1997 05:00:00 GMT);
  24. // Date in the pastheader(Pragma: no-cache);
  25. // set content type:
  26. header(Content-Type: text/html; charset=iso-8859-1);
  27. header(Content-Type: text/html; charset=utf-8);
  28. header(Content-Type: text/plain);
  29. // plain text file
  30. header(Content-Type: image/jpeg);
  31. // JPG picture
  32. header(Content-Type: application/zip);
  33. // ZIP file
  34. header(Content-Type: application/pdf);
  35. // PDF file
  36. header(Content-Type: audio/mpeg);
  37. // Audio MPEG (MP3,…) file
  38. header(Content-Type: application/x-shockwave-flash);
  39. // Flash animation// show sign in box
  40. header(HTTP/1.1 401 Unauthorized);
  41. header(WWW-Authenticate: Basic realm="Top Secret");
  42. print Text that will be displayed if the user hits cancel or ;
  43. print enters wrong login data;
  44. ?>
声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn核实处理。