Modify HTTP Headers (Examples) (PHP)


SUBMITTED BY: scripts4you

DATE: Oct. 21, 2015, 9:02 a.m.

FORMAT: Text only

SIZE: 2.6 kB

HITS: 1602

  1. // See related links for more status codes
  2. // Use this header instruction to fix 404 headers
  3. // produced by url rewriting...
  4. header('HTTP/1.1 200 OK');
  5. // Page was not found:
  6. header('HTTP/1.1 404 Not Found');
  7. // Access forbidden:
  8. header('HTTP/1.1 403 Forbidden');
  9. // The page moved permanently should be used for
  10. // all redrictions, because search engines know
  11. // what's going on and can easily update their urls.
  12. header('HTTP/1.1 301 Moved Permanently');
  13. // Server error
  14. header('HTTP/1.1 500 Internal Server Error');
  15. // Redirect to a new location:
  16. header('Location: http://www.example.org/');
  17. // Redriect with a delay:
  18. header('Refresh: 10; url=http://www.example.org/');
  19. print 'You will be redirected in 10 seconds';
  20. // you can also use the HTML syntax:
  21. // <meta http-equiv="refresh" content="10;http://www.example.org/ />
  22. // override X-Powered-By value
  23. header('X-Powered-By: PHP/4.4.0');
  24. header('X-Powered-By: Brain/0.6b');
  25. // content language (en = English)
  26. header('Content-language: en');
  27. // last modified (good for caching)
  28. $time = time() - 60; // or filemtime($fn), etc
  29. header('Last-Modified: '.gmdate('D, d M Y H:i:s', $time).' GMT');
  30. // header for telling the browser that the content
  31. // did not get changed
  32. header('HTTP/1.1 304 Not Modified');
  33. // set content length (good for caching):
  34. header('Content-Length: 1234');
  35. // Headers for an download:
  36. header('Content-Type: application/octet-stream');
  37. header('Content-Disposition: attachment; filename="example.zip"');
  38. header('Content-Transfer-Encoding: binary');
  39. // load the file to send:
  40. readfile('example.zip');
  41. // Disable caching of the current document:
  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. // set content type:
  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'); // plain text file
  49. header('Content-Type: image/jpeg'); // JPG picture
  50. header('Content-Type: application/zip'); // ZIP file
  51. header('Content-Type: application/pdf'); // PDF file
  52. header('Content-Type: audio/mpeg'); // Audio MPEG (MP3,...) file
  53. header('Content-Type: application/x-shockwave-flash'); // Flash animation
  54. // show sign in box
  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';

comments powered by Disqus