我的 users_model 中有一个名为 isLoggedin() 的静态函数,它需要我制作的库。我无法加载它!它位于名称 SessionsHelper.php 下的库文件夹中,其中有一个 SessionsHelper 类(我知道这并不奇怪)。我尝试将它加载到静态函数中,结果$this->load->library('SessionsHelper')
发现我不能在静态函数中使用 $this。然后我尝试使用require_once '../libraries/SessionsHelper.php'
不起作用的加载它,然后我尝试了自动加载器,它也不起作用....所以这里的功能。
public static function isLoggedin() {
if (SessionsHelper::get('id') != '' && SessionsHelper::get('id') >= 0) { // TODO add logic to differentiate between user/admin
return true;
}
return false;
}
我只需要能够使用图书馆!请任何帮助将不胜感激!
编辑:控制器是:
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();
}
}
模型是:
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];
}
}
该库在 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;
}
}