php isset中的非法偏移类型或为空 [英] php Illegal offset type in isset or empty

查看:253
本文介绍了php isset中的非法偏移类型或为空的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的代码最初有效,但在重新启动计算机后无效。

I have this code that was working initially, but does not work after I restarted my computer.

我得到的错误是:


警告:第4行的D:\ xampp \\\\\\\\\\\\\\\\\\\\\\\\\\ p>

Warning: Illegal offset type in isset or empty in D:\xampp\htdocs\cookieboylive\classes\Session.php on line 4

我网站上有3个文件 - 索引,登录,注册。索引页面检查用户是否已登录,但我认为它与问题无关。

There's 3 files on my site - Index, Login, Register. Index page checks if users is logged in, but I don't think it has anything to do with the problem.

以下是当前代码:

require_once 'core/init.php';

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()) {
            $user = new User();
            $login = $user->login(Input::get('username'), Input::get('password'));

            if($login) {
                Redirect::to('index.php');
            } else {
                echo '<p>Sorry, login failed.</p>';
            }
        } else {
            foreach($validation->errors() as $error) {
                echo $error, '<br>';
            }
        }
    }
}

if(Input::exists()) {
    if(Token::check(Input::get('token'))) {
        $validate = new Validate();
        $validation = $validate->check($_POST, array(
            'username' => array(
                'required' => true,
                'min' => 2,
                'max' => 20,
                'unique' => 'users'
            ),
            'password' => array(
                'required' => true,
                'min' => 6
            ),
            'password_again' => array(
                'required' => true,
                'matches' => 'password'
            ),
            'name' => array(
                'required' => true,
                'min' => 2,
                'max' => 50
            )
        ));

        if($validation->passed()) {
            $user = new User();

            $salt = Hash::salt(32);

            try {

                $user->create(array(
                    'username' => Input::get('username'),
                    'password' => Hash::make(Input::get('password'), $salt),
                    'salt' => $salt,
                    'name' => Input::get('name'),
                    'joined' => date('Y-m-d H:i:s'),
                    'group' => 1
                ));

                Session::flash('home', 'Register SUCCESS');
                Redirect::to('index.php');

            } catch(Exception $e) {
                die($e->getMessage());
            }
        } else {
            foreach($validation->errors() as $error) {
                echo $error, '<br>';
            }
        }

    }
}

?>

<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
        <meta name="viewport" content="width=device-width, initial-scale=1.0"> 
        <link rel="stylesheet" type="text/css" href="styles/register_login_styles.css">
        <link rel="stylesheet" type="text/css" href="styles/animate-custom.css">        
    </head>
    <body>
        <div class="container">
            <div id="container_demo" >
    <a class="hiddenanchor" id="toregister"></a>
    <a class="hiddenanchor" id="tologin"></a>
    <div id="wrapper">
        <div id="login" class="animate form">
            <form  action="" method="post">
                <div class="field">
                    <h1>Log in</h1>
                    <p>
                        <label for="username" class="uname" data-icon="u" >Username </label>
                        <input id="username" name="username" required="required" type="text" placeholder="Username" autocomplete="off">
                    </p>
                    <p>
                        <label for="password" class="youpasswd" data-icon="p">Password </label>
                        <input id="password" name="password" required="required" type="password" placeholder="Password">
                    </p>
                    <p class="keeplogin">
                        <input type="checkbox" name="loginkeeping" id="loginkeeping" value="loginkeeping">
                        <label for="loginkeeping">Keep me logged in</label>
                    </p>
                    <input type="hidden" name="token" value="<?php echo Token::generate(); ?>">
                    <p class="login button">
                        <input type="submit" value="Login">
                    </p>
                    <p class="change_link">
                        Not a member yet ?
                        <a href="#toregister" class="to_register">Join us</a>
                    </p>
                </form>
            </div>
        </div>

        <div id="register" class="animate form">
            <form  action="" method="post">
                <h1> Sign up </h1>
                <p>
                    <label for="username" class="uname" data-icon="u">Username</label>
                    <input id="username" value="<?php echo escape(Input::get('username')); ?>" name="username" required="required" type="text" placeholder="Username" autocomplete="off">
                </p>
                <p>
                    <label for="password" class="youpasswd" data-icon="p">Your password </label>
                    <input id="password" name="password" required="required" type="password" placeholder="Password">
                </p>
                <p>
                    <label for="password_again" class="youpasswd" data-icon="p">Please confirm your password </label>
                    <input id="password_again" name="password_again" required="required" type="password" placeholder="Password again">
                </p>
                <p>
                    <label for="name" class="youmail" data-icon="n" >Name</label>
                    <input id="name" name="name" value="<?php echo escape(Input::get('name')); ?>" required="required" type="text" placeholder="Name" autocomplete="off">
                </p>
                <input type="hidden" name="token" value="<?php echo Token::generate(); ?>">
                <p class="signin button">
                    <input type="submit" value="Sign up">
                </p>
                <p class="change_link"> 
                    Already a member ?
                    <a href="#tologin" class="to_register"> Go and log in </a>
                </p>
            </form>
        </div>

    </div>
</div>  
        </div>
    </body>
</html>



Session.php



Session.php

<?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);
        }
    }
}



User.php



User.php

<?php
class User {
    private $_db,
            $_data,
            $_sessionName,
            $_isLoggedIn;

    public function __construct($user = null) {
        $this->_db = DB::getInstance();

        $this->_sessionName = Config::get('session/session_name');

        if(!$user) {
            if(Session::exists($this->_sessionName)) {
                $user = Session::get($this->_sessionName);

                if($this->find($user)) {
                    $this->_isLoggedIn = true;
                } else {
                    // Pr0cess logout
                }

            } else {
                $this->find($user);
            }
        }
    }

    public function create($fields = array()) {
        if(!$this->_db->insert('users', $fields)) {
            throw new Exception('There was a problem creating an 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;
    }

    public function data() {
        return $this->_data;
    }

    public function isLoggedIn() {
        return $this->_isLoggedIn;
    }
}


推荐答案

我尝试了上面提出的大多数解决方案。事实上,答案不是在拼写中,而是如上所述,存在函数中的$ name变量实际上是一个数组。

I tried most of the solutions suggested above. In actual fact the answer is not in the spelling but is in the fact that, as was pointed out above, the $name variable in the exists function is actually an array.

存在公共静态函数($ name){
return(isset($ _ SESSION [$ name]))?真假;
}

简单的解决方法是将[0]追加到[$ name],使其成为[$ name] [0 ]返回与之关联的值和您想要查看的值。适用于我。为什么它在视频中起作用,我无法弄清楚;可能是配置事情。

The simple fix is to append [0] to [$name] so it becomes [$name][0] which returns the value associated with it and the one you want to see. Works for me. Why it worked in the video, I can't figure out; may be a configuration thing.

这篇关于php isset中的非法偏移类型或为空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆