I have a static function in my users_model, called isLoggedin(), that needs a library I made. I can't get it to load! It's in the libraries folder under the name SessionsHelper.php and has a class SessionsHelper in it(unsurprising I know). I tried loading it in the static function with $this->load->library('SessionsHelper')
only to find that I can't use $this in a static function. Then I tried to load it using require_once '../libraries/SessionsHelper.php'
which didn't work, then I tried the autoloader, which didn't work either.... so here's the function.
public static function isLoggedin() {
if (SessionsHelper::get('id') != '' && SessionsHelper::get('id') >= 0) { // TODO add logic to differentiate between user/admin
return true;
}
return false;
}
I just need to be able to use the library! Please any help would be very appreciative!
EDIT: The controller is:
class Users extends CI_Controller{
public function login(){
$this->load->model('users_model');
if($this->users_model->isLoggedin()){
redirect($this->config->base_url);
}
$data['info'] = $this->users_model->login('slavigne@uark.edu', 'abcdefg');
$this->load->view('templates/header');
$this->load->view('users/login', $data);
$this->load->view('templates/footer');
}
public function logout(){
$this->load->library('SessionHelper');
SessionsHelper::destroy();
}
}
the model is:
class users_model extends CI_Model{
public function users_model(){
parent::__construct();
}
public static function hash($password, $salt) {
//this code works no problem! but it's private you know?
}
public static function isLoggedin() {
$CI =& get_instance();
$CI->load->library('SessionsHelper');
if (SessionsHelper::get('id') != '' && SessionsHelper::get('id') >= 0) { // TODO add logic to differentiate between user/admin
return true;
}
return false;
}
public function login($email, $password){
$this->load->library('SessionHelper');
$this->load->database();
$query = $this->db->get_where('users', array('email' => $email));
$result = $query->result();
if($result[0]->password == $this->hash($password, $result[0]->salt)){
//error_log("Pass matched!");
SessionHelper::set('id', $result[0]->id);
}
return $result[0];
}
}
the library is and is in application/libraries/Sessinshelper.php:
class Sessionshelper {
public function __construct() {
}
public static function start() {
if (!session_id()) {
session_start();
}
}
public static function set($fld, $val) {
self::start();
$_SESSION[$fld] = $val;
}
public static function un_set($fld) {
self::start();
unset($_SESSION[$fld]);
}
public static function destroy() {
if (session_id() != "" || isset($_COOKIE[session_name()])) {
$params = session_get_cookie_params();
setcookie(session_name(), '', time() - 42000, $params["path"], $params["domain"], $params["secure"], $params["httponly"]);
}
session_unset();
$_SESSION = array();
session_destroy();
}
public static function get($fld) {
self::start();
if (isset($_SESSION[$fld])) {
return $_SESSION[$fld];
}
return FALSE;
}
}
You can load any library in a static context by loading the globally accessible CodeIgniter instance with get_instance
, resulting in:
$CI =& get_instance();
$CI->load->library('SessionHelper');
But static methods are usually considered by the community as hardly testable and design smells especially when using a framework like CodeIgniter which lets you avoid it.