2016-12-01 11 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

次のエラーの画像(リンク)はあります:[リンク](http://imgur.com/a/S5aoA) – astronomicalXoom

答えて

0

あなたは$ validate->最初の引数としてチェックして、それを$ _POST [「ログイン」]渡して呼んでいるが、あなたのHTMLに基づいてあなただけの$ _POSTを渡す必要があります。 $ _POST ["Login"]を渡すと、フォーム入力の属性名はname = "Login [username]"となります。

今あなたが$ _POSTを渡す[「ログイン」]その実際に空の配列、それはあなたが不正な文字列を取得している理由かもしれないので、エラーでファイル、およびルール番号があり

+0

私は 'var_dump($ _ POST ['Login;])))'と '値:ログイン:フォームを送信する入力ボタンの値ですか?これを修正する可能性はありますか? – astronomicalXoom

関連する問題