PageRenderTime 16ms CodeModel.GetById 12ms app.highlight 2ms 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 */
 10class Error {
 11
 12	/**
 13	 * Function check, checks if an error has been set.
 14	 *
 15	 * Using this class, errors are stored using _error_ as the prefix.
 16	 * This function will automatically use _error_ as a prefix for any
 17	 * requested names.
 18	 * eg. _error_login or _error_register or _error_general
 19	 *
 20	 * @param string $name The name of the error to be checked
 21	 * @param string $location The location (eg, in session or cookie). Default as session.
 22	 * @return boolean
 23	 */
 24	public static function check($name, $location = 'session')
 25	{
 26		switch($location){
 27			case 'session':
 28				if(isset($_SESSION['_error_'.$name])){
 29					return true;
 30				} else {
 31					return false;
 32				}
 33			break;
 34
 35			case 'cookie':
 36				if(isset($_COOKIE['_error_'.$name])){
 37					return true;
 38				} else {
 39					return false;
 40				}
 41			break;
 42
 43			default:
 44				return false;
 45			break;
 46		}
 47	}
 48
 49	/**
 50	 * Function get, will get the requested error message.
 51	 *
 52	 * Best used after Error::check();
 53	 * Example:
 54	 * <code>
 55	 *	if( Error::check('general') ){
 56	 *		echo Error::get('general');
 57	 *  }
 58	 * </code>
 59	 *
 60	 * Will return the value if available or false if not. If a value is returned
 61	 * the value will then be unset from the session or cookie so that it is only
 62	 * used once.
 63	 *
 64	 *
 65	 * @param string $name The name of the error to be returned
 66	 * @param string $location The location (eg, in session or cookie). Default as session.
 67	 * @return string/boolean
 68	 */
 69	public static function get($name, $location = 'session')
 70	{
 71		switch($location){
 72			case 'session':
 73				$error = $_SESSION['_error_'.$name];
 74				unset($_SESSION['_error_'.$name]);
 75				return $error;
 76			break;
 77
 78			case 'cookie':
 79				$error = $_COOKIE['_error_'.$name];
 80				unset($_COOKIE['_error_'.$name]);
 81				return $error;
 82			break;
 83
 84			default:
 85				return false;
 86			break;
 87		}
 88	}
 89
 90	/**
 91	 * Function to set a cookie or session as an error. Returns true if successful, false if not.
 92	 *
 93	 * @param string $name The name of the  error (excluding _error_).
 94	 * @param string $value The error message or value.
 95	 * @param string $location The location to store the error (session or cookie).
 96	 * @return boolean
 97	 */
 98	public static function set($name, $value, $location = 'session')
 99	{
100		switch($location){
101			case 'session':
102				$_SESSION['_error_'.$name] = $value;
103				return true;
104			break;
105			case 'cookie':
106				setcookie('_error_'.$name, $value);
107				return  true;
108			break;
109			default:
110				return false;
111			break;
112		}
113	}
114
115}