PageRenderTime 50ms CodeModel.GetById 25ms RepoModel.GetById 1ms app.codeStats 0ms

/application/library/error.php

https://github.com/jralph/4520-325
PHP | 115 lines | 59 code | 8 blank | 48 comment | 4 complexity | 85365e37a43a0ab9c26c9411fdad9911 MD5 | raw file
  1. <?php
  2. /**
  3. * The errors class is used to check if any errors have been activated and display them.
  4. * It also has a specific function to set an error to be displayed when the user next
  5. * loads the page.
  6. *
  7. * @author Joseph Ralph <jralph@arrowvaleacademy.co.uk>
  8. *
  9. */
  10. class Error {
  11. /**
  12. * Function check, checks if an error has been set.
  13. *
  14. * Using this class, errors are stored using _error_ as the prefix.
  15. * This function will automatically use _error_ as a prefix for any
  16. * requested names.
  17. * eg. _error_login or _error_register or _error_general
  18. *
  19. * @param string $name The name of the error to be checked
  20. * @param string $location The location (eg, in session or cookie). Default as session.
  21. * @return boolean
  22. */
  23. public static function check($name, $location = 'session')
  24. {
  25. switch($location){
  26. case 'session':
  27. if(isset($_SESSION['_error_'.$name])){
  28. return true;
  29. } else {
  30. return false;
  31. }
  32. break;
  33. case 'cookie':
  34. if(isset($_COOKIE['_error_'.$name])){
  35. return true;
  36. } else {
  37. return false;
  38. }
  39. break;
  40. default:
  41. return false;
  42. break;
  43. }
  44. }
  45. /**
  46. * Function get, will get the requested error message.
  47. *
  48. * Best used after Error::check();
  49. * Example:
  50. * <code>
  51. * if( Error::check('general') ){
  52. * echo Error::get('general');
  53. * }
  54. * </code>
  55. *
  56. * Will return the value if available or false if not. If a value is returned
  57. * the value will then be unset from the session or cookie so that it is only
  58. * used once.
  59. *
  60. *
  61. * @param string $name The name of the error to be returned
  62. * @param string $location The location (eg, in session or cookie). Default as session.
  63. * @return string/boolean
  64. */
  65. public static function get($name, $location = 'session')
  66. {
  67. switch($location){
  68. case 'session':
  69. $error = $_SESSION['_error_'.$name];
  70. unset($_SESSION['_error_'.$name]);
  71. return $error;
  72. break;
  73. case 'cookie':
  74. $error = $_COOKIE['_error_'.$name];
  75. unset($_COOKIE['_error_'.$name]);
  76. return $error;
  77. break;
  78. default:
  79. return false;
  80. break;
  81. }
  82. }
  83. /**
  84. * Function to set a cookie or session as an error. Returns true if successful, false if not.
  85. *
  86. * @param string $name The name of the error (excluding _error_).
  87. * @param string $value The error message or value.
  88. * @param string $location The location to store the error (session or cookie).
  89. * @return boolean
  90. */
  91. public static function set($name, $value, $location = 'session')
  92. {
  93. switch($location){
  94. case 'session':
  95. $_SESSION['_error_'.$name] = $value;
  96. return true;
  97. break;
  98. case 'cookie':
  99. setcookie('_error_'.$name, $value);
  100. return true;
  101. break;
  102. default:
  103. return false;
  104. break;
  105. }
  106. }
  107. }