2012-07-19 58 views
0

我真的搞砸了。有沒有人可以幫助我,使用php進行服務器端驗證的更簡單的方法或簡單的方法是什麼? 我真的很感激.....簡單的服務器端驗證註冊表單在PHP(不同的技術)

+1

沒有「簡單」的方式,因爲每個人的驗證要求都不一樣。 – 2012-07-19 17:26:24

+0

這個答案可能會幫助你:http://stackoverflow.com/questions/737385/easiest-form-validation-library-for-php – redbirdo 2012-07-19 17:35:24

+0

請具體。 Zend Framework有[Zend_Validate](http://framework.zend.com/manual/en/zend.validate.introduction.html)。 – 2012-07-19 17:36:08

回答

0

正如馬克乙說,真的沒有簡單的方式。您可以使用第三方表單驗證庫/類,或者使用具有表單驗證功能的框架作爲組件。我個人使用Zend Framework,但這對你來說可能是矯枉過正的。

如果你這樣做你自己,你要記住以下(加上更多取決於你的需求):

  • 是根據特定的期望格式的數據是否正確? (即電子郵件,電話號碼,郵政編碼)您可以通過多種方式進行驗證。 (正則表達式)
  • 您是插入數據庫還是回到頁面?那麼你應該逃脫。簡單的方法包括:htmlentities,htmlspecialchars等。再次,這取決於您的具體需求
  • 是否有您想強制執行的最大限制?如果是這樣,你需要做這個服務器端,因爲DOM可以由用戶操縱,任何input級別的限制可以很容易地改變。
  • 是否需要填寫?確保POST變量存在並且不爲空。

你只需要寫下你的表單需求,然後從那裏決定你將如何實現驗證。

0

下面是一個簡單的PHP表單驗證:

<?php 

    // validate $_GET['foo'], here I test that it is at least 1 character long 
    $valid = TRUE; 
    if(isset($_GET['foo']) && strlen($_GET['foo']) < 1) { 
     $valid = FALSE; 
    } 

    // if the form is submitted and $_GET['foo'] is valid, show a success message 
    if(isset($_GET['bar']) && $valid) : 

     // do something with the form data 
?> 

    <p>The form was successfully submitted.</p> 

<?php 
    // show the form 
    else : 
?> 

    <form method="get" action=""> 
     <div> 
      <label for="foo">Foo: </label> 
      <input type="text" id="foo" name="foo" value="<?php echo $_GET['foo']; ?>" /> 
      <?php if(!$valid) : ?> 
       <p>Please enter a valid value.</p> 
      <?php endif; ?> 
     </div> 
     <div> 
      <input type="submit" id="bar" name="bar" value="Bar" /> 
     </div> 
    </form> 

<?php 
    endif; 
?> 
0

使用此:

<?php 

class Validator { 

    public $data = array(); 
    public $rules = array(); 
    public $messages = array(); 
    public $error = array(); 
    public $pass = true; 

    public function __construct($options = null) { 

     foreach ($options as $option => $value) { 

     $this->$option = $value; 

     } 

    } 

    public function validate() { 

    foreach($this->rules as $k=>$v) { 

     $rules = explode('|',$v); 
     $pass = true; 

     foreach ($rules as $rule) { 

     $rule = explode(':',$rule); 

     switch ($rule[0]) { 

      case 'required': 
      if(empty($this->data[$k])) { 
       $pass = false; 
      } 
      break; 

      case 'min': 
      if(strlen($this->data[$k]) < $rule[1]) { 
       $pass = false; 
      } 
      break; 

      case 'max': 
      if(strlen($this->data[$k]) > $rule[1]) { 
       $pass = false; 
      } 
      break; 

      case 'equal': 
      if(strlen($this->data[$k]) != $rule[1]) { 
       $pass = false; 
      } 
      break; 

      case 'not': 
      if($this->data[$k] == $rule[1]) { 
       $pass = false; 
      } 
      break; 

      case 'allow': 
      $allowed = explode(',',$rule[1]); 
      if(!in_array($this->data[$k],$allowed)) { 
       $pass = false; 
      } 
      break; 

      case 'email': 
      if (!filter_var($this->data[$k],FILTER_VALIDATE_EMAIL)) { 
       $pass = false; 
      } 
      break; 

      case 'same': 
      if ($this->data[$k] != $this->data[$rule[1]]) { 
       if(!isset($this->error[$rule[1]])) { 
       $this->error[$rule[1]] = ''; 
       } 
       $pass = false; 
      } else { 
       if(isset($this->error[$rule[1]])) { 
       $this->error[$k] = $this->error[$rule[1]]; 
       } 
      } 
      break; 

      case 'unique': 
      if (egrediDB_checkUnique($rule[1],$k,$this->data[$k]) != 0) { 
       $pass = false; 
      } 

     } 

     if($pass == false) { 

      $this->pass = false; 
      $this->error[$k] = $this->messages[$k]; 

     } 

     } 

    } 

    //print_r($this->error); 

    return array(
     'error'=>$this->error, 
     'pass'=>$this->pass 
    ); 

    } 

} 

?> 

注:我做了一個自定義函數的數據庫檢查:

<?php 

// Funktion für die Klasse Validator.php 
function egrediDB_checkUnique($table,$column,$value) { 

    global $egrediDB; 

    return $egrediDB->count($table,array($column=>$value)); 

} 

?> 


<?php 

$Validator = new Validator(array(
    'data'=>$_POST['data'], 
    'rules' => array(
    'firstname'  => 'required', 
    'lastname'  => 'required', 
    'password'  => 'required|min:8', 
    'password_repeat' => 'required|same:password', 
    'email'   => 'email|unique:user', 
    'email_repeat' => 'required|same:email', 
    'timezone'  => 'required|not:-1', 
    'country'   => 'required|not:-1|max:2', 
    'street'   => 'required', 
    'postalcode'  => 'required', 
    'city'   => 'required', 
), 
    'messages' => array(
    'firstname'  => 'Bitte geben Sie ein Vornamen ein', 
    'lastname'  => 'Bitte geben Sie ein Nachnamen ein', 
    'password'  => 'Bitte geben Sie ein Sicheres 8-stelliges Passwort ein', 
    'password_repeat' => 'Ihre Passwörter stimmen nicht überein', 
    'email'   => 'E-Mail ist ungültig oder schon registriert', 
    'email_repeat' => 'Ihre E-Mails stimmen nicht überein', 
    'timezone'  => 'Bitte wählen Sie eine Zeitzone', 
    'country'   => 'Bitte wählen Sie ein Land', 
    'street'   => 'required', 
    'postalcode'  => 'required', 
    'city'   => 'required', 
), 
)); 

echo json_encode($Validator->validate());exit; 

?>