From d70e6f21ce4b90631413c242fb76e39261704383 Mon Sep 17 00:00:00 2001 From: Alex Tselegidis Date: Sat, 23 Oct 2021 13:18:20 +0200 Subject: [PATCH] Added helper file with session related functions. --- application/helpers/session_helper.php | 60 ++++++++++++++++++++++++++ 1 file changed, 60 insertions(+) create mode 100644 application/helpers/session_helper.php diff --git a/application/helpers/session_helper.php b/application/helpers/session_helper.php new file mode 100644 index 00000000..72753934 --- /dev/null +++ b/application/helpers/session_helper.php @@ -0,0 +1,60 @@ + + * @copyright Copyright (c) 2013 - 2020, Alex Tselegidis + * @license http://opensource.org/licenses/GPL-3.0 - GPLv3 + * @link http://easyappointments.org + * @since v1.3.0 + * ---------------------------------------------------------------------------- */ + +if ( ! function_exists('session')) +{ + /** + * Get / set the specified session value. + * + * If an array is passed as the key, we will assume you want to set an array of values. + * + * Example "Get": + * + * $logged_in = session('logged_in', FALSE); + * + * Example "Set": + * + * session(['logged_in' => FALSE]); + * + * @param array|string $key + * @param mixed $default + * + * @return mixed|NULL Returns the configuration value or NULL if setting the configuration key. + * + * @throws InvalidArgumentException + */ + function session($key = NULL, $default = NULL) + { + /** @var EA_Controller $CI */ + $CI = &get_instance(); + + if (empty($key)) + { + throw new InvalidArgumentException('The $key argument cannot be empty.'); + } + + if (is_array($key)) + { + foreach ($key as $item => $value) + { + $CI->session->set_userdata($item, $value); + } + + return NULL; + } + + $value = $CI->session->userdata($key); + + return $value ?? $default; + } +}