php Illegal offset type in isset or empty
我的这段代码最初可以运行,但在我重新启动计算机后无法运行。
我得到的错误是:
Warning: Illegal offset type in isset or empty in D:\\xampp\\htdocs\\cookieboylive\\classes\\Session.php on line 4
我的网站上有 3 个文件 - 索引、登录、注册。索引页面检查用户是否登录,但我认为这与问题无关。
这是当前代码:
主 register/login.php 页面
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 | 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, ''; } } } } 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, ''; } } } } ?> <!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> <form action="" method="post"> Log in <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 ? Join us </p> </form> <form action="" method="post"> Sign up <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><center>[wp_ad_camp_2]</center></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 ? Go and log in </p> </form> </body> </html> |
会话.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 | <?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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 | <?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; } } |
我尝试了上面建议的大多数解决方案。实际上,答案不在于拼写,而在于事实上,正如上面所指出的,exists 函数中的 $name 变量实际上是一个数组。
1 2 3 |
简单的解决方法是将 [0] 附加到 [$name],使其变为 [$name][0],它返回与其关联的值以及您想要查看的值。为我工作。为什么它在视频中起作用,我不知道;可能是配置的东西。
I'm getting Fatal error: DEBUG: array ...
表示
我了解您正在处理的上下文,Skillfeed 的一个教程 PHP 登录/注册项目。事实上,我遇到了同样的错误,导致我在谷歌上搜索了同样的错误——我在你的这个 stackoverflow 线程上结束了。
我找不到很有帮助的答案,但我查看了我项目的课程,发现问题的根源在另一个课程中,而不是此处显示的 3 个课程。
在 Token.php 中:
1 2 3 4 5 6 7 8 9 10 | public static function check($token){ $tokenName = Config::get('sesson/token_name'); if(Session::exists($tokenName) && $token === Session::get($tokenName)){ Session::delete($tokenName); return true; } return false; } |
问题是一个简单的拼写错误,注意 get() 函数的字符串参数"sesson/token_name",它应该是"session/token_name"。一旦我解决了这个问题,它就对我有用!