CI =& get_instance(); foreach (array('sess_cookie_name', 'cookie_prefix', 'cookie_domain', 'cookie_path') as $key) { $this->$key = isset($params[$key]) ? $params[$key] : $this->CI->config->item($key); } // Set the cookie name $this->sess_cookie_name = $this->cookie_prefix . $this->sess_cookie_name; // Get the session data $this->_cookie_read(); } /** * Add or change data in the "userdata" array */ function set_userdata($newdata = array(), $newval = '') { if (is_string($newdata)) { $newdata = array($newdata => $newval); } if (count($newdata) > 0) { foreach ($newdata as $key => $val) { $this->userdata[$key] = $val; } } } /** * Delete a session variable from the "userdata" array */ function unset_userdata($newdata = array()) { if (is_string($newdata)) { $newdata = array($newdata => ''); } if (count($newdata) > 0) { foreach ($newdata as $key => $val) { unset($this->newdata[$key]); } } } /** * Fetch a specific item from the session array */ function userdata($item) { return isset($this->userdata[$item]) ? $this->userdata[$item] : FALSE; } /** * Fetch all session data */ function all_userdata() { return isset($this->userdata) ? $this->userdata : FALSE; } /** * Create a new session */ function sess_create($userdata = NULL, $expire = 0) { if (is_array($userdata) && count($userdata)) $this->set_userdata($userdata); $userdata = serialize($this->userdata); $sess_expiration = $expire ? $expire + time() : 0; // Set the cookie setcookie( $this->sess_cookie_name, $userdata, $sess_expiration, $this->cookie_path, $this->cookie_domain ); } /** * Destroy the current session */ function sess_destroy() { // Kill the cookie setcookie( $this->sess_cookie_name, '', time() - (60 * 60 * 24 * 30), $this->cookie_path, $this->cookie_domain ); } /** * Fetch the current session data if it exists */ private function _cookie_read() { $session = $this->CI->input->cookie($this->sess_cookie_name); if ($session === FALSE) { return FALSE; } $session = unserialize($session); if (!is_array($session)) { $this->sess_destroy(); return FALSE; } // Session is valid $this->userdata = $session; return TRUE; } } // END Session Class // // /* End of file Session.php */ // /* Location: ./application/libraries/Session.php */