Modify HTTP Headers (Examples)


/ Published in: PHP
Save to your folder(s)

Many examples that show how to use the header() function of PHP


Copy this code and paste it in your HTML
  1. // See related links for more status codes
  2.  
  3. // Use this header instruction to fix 404 headers
  4. // produced by url rewriting...
  5. header('HTTP/1.1 200 OK');
  6.  
  7. // Page was not found:
  8. header('HTTP/1.1 404 Not Found');
  9.  
  10. // Access forbidden:
  11. header('HTTP/1.1 403 Forbidden');
  12.  
  13. // The page moved permanently should be used for
  14. // all redrictions, because search engines know
  15. // what's going on and can easily update their urls.
  16. header('HTTP/1.1 301 Moved Permanently');
  17.  
  18. // Server error
  19. header('HTTP/1.1 500 Internal Server Error');
  20.  
  21. // Redirect to a new location:
  22. header('Location: http://www.example.org/');
  23.  
  24. // Redriect with a delay:
  25. header('Refresh: 10; url=http://www.example.org/');
  26. print 'You will be redirected in 10 seconds';
  27.  
  28. // you can also use the HTML syntax:
  29. // <meta http-equiv="refresh" content="10;http://www.example.org/ />
  30.  
  31. // override X-Powered-By value
  32. header('X-Powered-By: PHP/4.4.0');
  33. header('X-Powered-By: Brain/0.6b');
  34.  
  35. // content language (en = English)
  36. header('Content-language: en');
  37.  
  38. // last modified (good for caching)
  39. $time = time() - 60; // or filemtime($fn), etc
  40. header('Last-Modified: '.gmdate('D, d M Y H:i:s', $time).' GMT');
  41.  
  42. // header for telling the browser that the content
  43. // did not get changed
  44. header('HTTP/1.1 304 Not Modified');
  45.  
  46. // set content length (good for caching):
  47. header('Content-Length: 1234');
  48.  
  49. // Headers for an download:
  50. header('Content-Type: application/octet-stream');
  51. header('Content-Disposition: attachment; filename="example.zip"');
  52. header('Content-Transfer-Encoding: binary');
  53. // load the file to send:
  54. readfile('example.zip');
  55.  
  56. // Disable caching of the current document:
  57. header('Cache-Control: no-cache, no-store, max-age=0, must-revalidate');
  58. header('Expires: Mon, 26 Jul 1997 05:00:00 GMT'); // Date in the past
  59. header('Pragma: no-cache');
  60.  
  61. // set content type:
  62. header('Content-Type: text/html; charset=iso-8859-1');
  63. header('Content-Type: text/html; charset=utf-8');
  64. header('Content-Type: text/plain'); // plain text file
  65. header('Content-Type: image/jpeg'); // JPG picture
  66. header('Content-Type: application/zip'); // ZIP file
  67. header('Content-Type: application/pdf'); // PDF file
  68. header('Content-Type: audio/mpeg'); // Audio MPEG (MP3,...) file
  69. header('Content-Type: application/x-shockwave-flash'); // Flash animation
  70.  
  71. // show sign in box
  72. header('HTTP/1.1 401 Unauthorized');
  73. header('WWW-Authenticate: Basic realm="Top Secret"');
  74. print 'Text that will be displayed if the user hits cancel or ';
  75. print 'enters wrong login data';

URL: http://www.jonasjohn.de/snippets/php/headers.htm

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.