2016-12-01 54 views
1

我正在爲我的網站製作驗證系統。實際上,如果我沒有在我的$_POST(如$_POST['Login'])中添加任何參數,我的代碼就可以工作。如果我把任何參數在我$_POST,則返回錯誤:非法字符串偏移和驗證在PHP中不起作用

Warning: Illegal string offset: 'username' in 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> 

如果提交表單將被處理的腳本:

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

    } 
+0

有一個文件,並在錯誤的規則數的原因。你能告訴我這是誰的規則嗎(不是規則號碼)? – DevNiels

+0

這是以下錯誤的圖片(鏈接):[link](http://imgur.com/a/S5aoA) – astronomicalXoom

回答

0

要調用$ validate->檢查並傳遞$ _ POST [「登錄」作爲第一個參數,但根據你的HTML你應該只通過$ _POST。當您傳遞$ _POST [「Login」]時,表單輸入應該具有屬性名稱,如name =「Login [username]」。

現在當你通過$ _ POST [「登錄」]自己居然空數組,這樣可能是爲什麼你正在非法串偏移

+0

我試圖'死(var_dump($ _ POST ['Login;]));'和它顯示:value:Login,這是提交表單的輸入按鈕的值?解決這個問題的任何機會? – astronomicalXoom