2012-07-19 59 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; 

?>