警告:非法偏移类型 php


Warning: Illegal offset type php

我一直在尝试使用 php oop 制作登录系统,但是我遇到了此错误警告:非法偏移类型....类''会话.php在第 11 行 我不太确定我哪里出错了,任何建议将不胜感激。

登录

if(Input::exists()){
    if(Token::check(Input::get('token'))){
        $validate = new Validate();
        $validation = $validate->check($_POST, array(
            'username' => array('required' => true),
            'password' => array('required' => true)
        ));
        if ($validation->passed()){
            //log user in
            $user = new User();
            $login = $user->login(Input::get('username'), Input::get('password'));
            if($login){
                echo 'Success';
            }else{
                echo'<p>Sorry invalid details</p>';
            }
        } else{
            foreach($validation->errors() as $error)
                echo $error, '<br />';
        }
    }
}
?>
<form action="" method="POST">
    <div class="field">
        <label for="username" id="username"> Username </label>
        <input type="text" name="username" id="username" autocomplete="off">
    </div>
    <div class="field">
        <label for="password" id="password"> Password </label>
        <input type="password" name="password" id="password" autocomplete="off">
    </div>
    <input type="hidden" name="token" value="<?php echo Token::generate();?>">
    <input type="submit" value="Login">
</form>  

用户

<?php
class User{
    private $_db,
            $_data,
            $_sessionName;
    public function __construct($user = null){
        $this ->_db = DB::getInstance();
        $this->_sessionName = Config::get('session/session_name');
    }
    public function create($fields = array()){
        if($this->_db->insert('users', $fields)){
            throw new Exception('There was a problem creating account');
        }
    }
    public function find($user = null){
        if($user){
            $field = (is_numeric($user)) ? 'id' : 'username';
            $data = $this->_db->get('users', array($field, '=', $user));
            if($data->count()) {
                $this->_data = $data->first();
                return true;
            }
        }
        return false;
    }
    public function login($username = null, $password = null){
            $user = $this->find($username);
        if($user){
            if($this->data()->password ===Hash::make($password, $this->data()->salt)){
                Session::put($this->_sessionName, $this-data()->id);
                return true;
            }
        }       
        return false;
    }
    private function data(){
        return $this->_data;
    }
}

会期

<?php
class Session {
    public static function exists($name){
        return(isset($_SESSION[$name])) ? true : false;
    }
    public static function put($name, $value){
        return $_SESSION[$name] = $value;
    }
    public static function get($name){
        return $_SESSION[$name];
    }
    public static function delete($name){
        if(self::exists($name)){
            unset($_SESSION[$name]);
        }
    }
    public static function flash($name, $string =''){
        if(self::exists($name)){
            $session = self::get($name);
            self::delete($name);
            return $session;
        } else {
            self::put($name, $string);
        }
    }
}

指数

<?php
    require_once 'core/init.php';
if(Session::exists('home')){
    echo '<p>' . Session::flash('home', 'You have been registered and can now log in!') . '</p>';
}
echo Session::get(Config::get('session/session_name'));
?>

在尝试访问会话密钥之前,您应该检查会话密钥是否存在:

<?php
class Session {
    public static function put($name, $value){
        return $_SESSION[$name] = $value;
    }
    public static function get($name)
    {
        return self::exists($name) ? $_SESSION[$name] : null;
    }
    public static function exists($name)
    {
        return @$_SESSION[$name] !== null;
    }
    public static function delete($name){
        if(self::exists($name)){
            unset($_SESSION[$name]);
        }
    }
    public static function flash($name, $string =''){
        if(self::exists($name)){
            $session = self::get($name);
            self::delete($name);
            return $session;
        } else {
            self::put($name, $string);
        }
    }
}