非法字符串偏移和验证在PHP中不起作用

非法字符串偏移和验证在PHP中不起作用,php,Php,我正在为我的网站制作一个验证系统。事实上,如果我没有在我的$\u POST中添加任何参数(比如$\u POST['Login']),我的代码就可以工作。如果我在我的$\u POST中放入任何参数,它将返回一个错误: 警告:非法的字符串偏移量:C:\ 我的认证表格示例: <form action="" method="post"> <div class="field"> <label for="username">Username: </la

我正在为我的网站制作一个验证系统。事实上,如果我没有在我的
$\u POST
中添加任何参数(比如
$\u POST['Login']
),我的代码就可以工作。如果我在我的
$\u POST
中放入任何参数,它将返回一个错误:

警告:非法的字符串偏移量:C:\

我的认证表格示例:

 <form action="" method="post">
  <div class="field">
    <label for="username">Username: </label>
    <input type="text" name="username" id="username" autocomplete="off" />
  </div>

  <div class="field">
    <label for="Password">Password: </label>
    <input type="password" name="password" id="password" autocomplete="off" />
  </div>

  <div class="field">
    <label for="remember">
      <input type="checkbox" name="remember" id="remember" value="on"/> Remember Me
    </label>
  </div>

  <input type="hidden" name="token" value="<?php echo Token::generate(); ?>" />
  <input type="submit" value="Login" name="Login"/>
</form>

用户名:
密码:
记得我吗
验证类:


您正在调用$validate->check并将其作为第一个参数传递给$\u POST[“Login”],但基于您的HTML,您应该只传递$\u POST。当您传递$_POST[“Login”]时,表单输入的属性名称应为name=“Login[username]”


现在,当您传递$\u POST[“Login”]时,它实际上是一个空数组,因此这可能是您获取非法字符串偏移量的原因。您调用$validate->check并将其作为第一个参数传递$\u POST[“Login”],但根据您的HTML,您应该只传递$\u POST。当您传递$_POST[“Login”]时,表单输入的属性名称应为name=“Login[username]”


现在,当您传递$_POST[“Login”]时,它实际上是一个空数组,因此这可能是您获取非法字符串偏移量的原因。

错误中有一个文件和规则编号。你能告诉我它是什么规则吗(不是规则编号)?下面是以下错误的图片(链接):错误中有一个文件和规则编号。你能告诉我这是什么规则吗(不是规则编号)?下面是以下错误的图片(链接):我试图
die(var_dump($_POST['Login;])并显示:value:Login,这是提交表单的输入按钮的值?有没有可能解决这个问题?我试图
die(var_dump($\u POST['Login;])并显示:value:Login,这是提交表单的输入按钮的值?有没有可能解决这个问题?
<?php
    require_once 'init.php';
    $user = new User();
    if($user->isLoggedIn()){
      Redirect::to('index.php');
    }
    $validate = new Validate();
    if(Input::exists()) {
        if(Token::check(Input::get('token'))) {
            $validation = $validate->check($_POST["Login"], array(
                'username' => array('required' => true),
                'password' => array('required' => true)
            ));
        }
    }
?>
<?php
    class Validate {

        # Set the variables
        private $_passed = false,
                $_errors = array(),
                $_db = null;

        # Construct or establish connection to the database
        public function __construct(){
            $this->_db = Database::getInstance();
        }

        # The validation/checking code or the main brain of the code
        public function check($source, $items = array()){
            # Run a ` for each ` for each item in the fields
            foreach($items as $item => $rules) {
                # Run a ` for each ` for every rule in the items
                foreach($rules as $rule => $rule_value) {
                    # Set the variables of `value` and `item`
                    $value = $source[$item];
                    $item = sanitize($item);

                    if($rule === 'required' && empty($value)) {
                        $this->addError("{$item} is required");
                    } else if (!empty($value)) {
                        switch($rule) {
                            # Case: Minimum
                            case 'min':
                                if(strlen($value) < $rule_value) {
                                    $this->addError("{$item} must be a minimum of {$rule_value} characters.");
                                }
                                break;

                            # Case Maximum
                            case 'max':
                                if(strlen($value) > $rule_value) {
                                    $this->addError("{$item} must be a maximum of {$rule_value} characters.");
                                }
                                break;

                            # Case: Match
                            case 'matches':
                                if($value != $source[$rule_value]) {
                                    $this->addError("{$rule_value} must match {$item}.");
                                }
                                break;

                            # Case: Unique
                            case 'unique':
                                $check = $this->_db->get($rule_value, array($item, '=', $value));

                                if($check->count()) {
                                    $this->addError("{$item} already exists.");
                                }
                                break;
                            # Case: Not match
                            case 'notmatch':
                              if($value === $source[$rule_value]) {
                                $this->addError("{$rule_value} must not match {$item}.");
                              }
                            break;
                        }
                    }
                }
            }

            if(empty($this->_errors)) {
                $this->_passed = true;
            }
        }

        # ~ ADD ~ and error
        public function addError($error) {
            $this->_errors[] = $error;
        }

        # ~ RETURN ~ the errors
        public function errors() {
            return $this->_errors;
        }

        # ~ CHECK ~ if it is passed
        public function passed() {
            return $this->_passed;
        }

    }