Home  >  Article  >  Backend Development  >  PHP header function usage example (1)

PHP header function usage example (1)

WBOY
WBOYOriginal
2016-07-25 08:56:20877browse
  1. print ""; //Usually the cache file is read

  2. ?>
  3. print" "; //Added a unique number to make the browser re-request

  4. w//print" ";

  5. ?>

Copy the code

2, customize the php function, and copy the image Sent to browser for display.

  1. function PE_img_by_path($PE_imgpath = “”)
  2. {
  3. if (file_exists($PE_imgpath)) {
  4. $PE_imgarray = pathinfo($PE_imgpath);
  5. $iconcontent = file_get_contents($PE_img path );
  6. header("Content-type: image/" . $PE_imgarray["extension"]);
  7. header('Content-length: ' . strlen($iconcontent));
  8. echo $iconcontent;
  9. die(0) ;
  10. }
  11. return false;
  12. }
  13. ?>
Copy code

More examples:

  1. // ok
  2. header('HTTP/1.1 200 OK');
  3. //Set a 404 header:
  4. header('HTTP/1.1 404 Not Found');
  5. // Set the address to be permanently redirected
  6. header('HTTP/1.1 301 Moved Permanently');
  7. //Go to a new address
  8. header('Location: http://bbs.it-home.org/');
  9. //File delayed redirection:
  10. header('Refresh: 10; url=http://bbs.it-home.org/');
  11. print 'You will be redirected in 10 seconds';
  12. //Of course, it can also Implemented using html syntax
  13. //
  14. // override X-Powered-By: PHP:
  15. header('X-Powered-By: PHP/4.4.0′);
  16. header('X-Powered-By: Brain/0.6b');
  17. //Document language
  18. header('Content-language: en') ;
  19. //Tell the browser the last modification time
  20. $time = time() – 60; // or filemtime($fn), etc
  21. header('Last-Modified: '.gmdate('D, d M Y H:i :s', $time).' GMT');
  22. //Tell the browser that the document content has not changed
  23. header('HTTP/1.1 304 Not Modified');
  24. //Set the content length
  25. header('Content-Length : 1234′);
  26. //Set as a download type
  27. header('Content-Type: application/octet-stream');
  28. header('Content-Disposition: attachment; filename="example.zip"');
  29. header('Content-Transfer-Encoding: binary');
  30. // load the file to send:
  31. readfile('example.zip');
  32. // disable caching for the current document
  33. header('Cache-Control: no- cache, no-store, max-age=0, must-ridate');
  34. header('Expires: Mon, 26 Jul 1997 05:00:00 GMT'); // Date in the past
  35. header('Pragma: no-cache');
  36. //Set content type:
  37. header('Content-Type: text/html; charset=iso-8859-1′);
  38. header('Content-Type: text/html; charset=utf -8′);
  39. header('Content-Type: text/plain'); //Plain text format
  40. header('Content-Type: image/jpeg'); //JPG image
  41. header('Content-Type: application/zip'); // ZIP file
  42. header('Content-Type: application/pdf'); // PDF file
  43. header('Content-Type: audio/mpeg'); // Audio file
  44. header(' Content-Type: application/x-shockwave-flash'); //Flash animation
  45. //Display login dialog box
  46. header('HTTP/1.1 401 Unauthorized');
  47. header('WWW-Authenticate: Basic realm=”Top Secret"');
  48. print 'Text that will be displayed if the user hits cancel or ';
  49. print 'enters wrong login data';
  50. ?>
Copy code


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn