header.class.php 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. <?php
  2. /**
  3. * Header utility functions.
  4. *
  5. * @license see /license.txt
  6. * @author Laurent Opprecht <laurent@opprecht.info> for the Univesity of Geneva
  7. */
  8. class Header
  9. {
  10. public static function response_code($response_code)
  11. {
  12. if (function_exists('http_response_code')) {
  13. http_response_code($response_code);
  14. return;
  15. }
  16. switch ($response_code) {
  17. case 400:
  18. header("HTTP/1.0 $response_code Bad Request");
  19. case 401:
  20. header("HTTP/1.0 $response_code Unauthorized");
  21. case 402:
  22. header("HTTP/1.0 $response_code Payment Required");
  23. case 403:
  24. header("HTTP/1.0 $response_code Forbidden");
  25. case 404:
  26. header("HTTP/1.0 $response_code Not Found");
  27. default:
  28. header("HTTP/1.0 $response_code");
  29. }
  30. }
  31. public static function content_type($mime_type, $charset = '')
  32. {
  33. if (empty($mime_type)) {
  34. return;
  35. }
  36. $type = $charset ? "$mime_type;charset=$charset" : $mime_type;
  37. header('Content-type: ' . $type);
  38. }
  39. public static function content_type_xml()
  40. {
  41. header('Content-type: text/xml');
  42. }
  43. public static function content_type_json()
  44. {
  45. header('Content-type: application/json');
  46. }
  47. public static function content_type_javascript()
  48. {
  49. header('Content-type: application/javascript');
  50. }
  51. /**
  52. * Redirect the navigator to the specified url.
  53. *
  54. * @param string $url
  55. */
  56. public static function location($url)
  57. {
  58. header("Location: $url");
  59. exit;
  60. }
  61. public static function expires($timestamp)
  62. {
  63. $value = gmdate('D, d M Y H:i:s \G\M\T', $timestamp);
  64. header('Expires: ' . $value);
  65. }
  66. public static function cache_control($value)
  67. {
  68. header('Cache-Control: ' . $value);
  69. }
  70. public static function pragma($value)
  71. {
  72. header('Pragma: ' . $value);
  73. }
  74. }