程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 網頁編程 >> PHP編程 >> 關於PHP編程 >> php header函數使用實例代碼

php header函數使用實例代碼

編輯:關於PHP編程

  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. ?>

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved