【问题标题】:Validation Layer in a PHP ClassPHP 类中的验证层
【发布时间】:2011-11-23 20:07:48
【问题描述】:

我需要帮助理解处理验证用户输入的逻辑。

我目前验证用户数据的状态是最糟糕的,使用这些凌乱的代码行我觉得很尴尬,看看我的典型函数,我用它来获取用户的输入并将其处理到数据库。

public function saveUser($arguments = array()) {
    //Check if $arguments have all the required values
    if($this->isRequired(array('name','email','password','pPhone','gender','roleId'))) {
        //$name should could minimum of 5 and maximum of 25 chars, and is a strict character.
        $name = $this->isString(5, 25, $this->data['name'], 'STRICT_CHAR');
        $email = $this->isEmail($this->data['email']);
        $pPhone = $this->isString(5, 12, $this->data['pPhone'], 'STRICT_NUMBER');
        $sPhone = (!empty($this->data['sPhone'])) ? $this->isString(5, 12, $this->data['sPhone'], 'STRICT_NUMBER') : 0;
        //Check For Duplicate Email Value
        $this->duplicate('user_details','email',$email);
        //If Static Variable $error is not empty return false
        if(!empty(Validation::$error)) { return false; }
        //After Validation Insert the value into the database.
        $sth = $this->dbh->prepare('INSERT QUERY');
        $sth->execute();
    }
}

现在是我专注于改进验证代码的时候了。我希望我的所有类方法在插入数据库之前验证用户输入。基本上一个接受用户输入的类方法应该能够执行以下操作。

  1. 如果类方法接受用户输入作为数组,则检查数组中的所有必需输入。

  2. 检查输入的最小和最大长度。

  3. 检查任何非法字符。

等等

我想知道如何处理这个问题,以及是否有任何 PHP 独立验证组件可以帮助我。这会有很大帮助。如果我做错了,请建议我改进我的代码,只要它保证我的代码遵循编码标准,我不介意做任何事情。

如果有人能向我解释处理验证逻辑以验证对象的类方法的用户输入,我将不胜感激。

谢谢你..

【问题讨论】:

    标签: php validation


    【解决方案1】:

    PHP 5.2 有一个名为“过滤器函数”的新核心扩展。您可以使用此扩展程序来清理和验证用户数据。

    例如,验证电子邮件地址:

    if (filter_var($email, FILTER_VALIDATE_EMAIL)) {
        echo "This (email) email address is considered valid.";
    }
    

    对于一般的验证处理,您希望将验证过程与传入数据和对象本身分离。我使用 Lithium PHP 框架,它们的数据验证类被实现为一个几乎独立的实用程序类。查看有关如何推出自己的想法的想法:http://li3.me/docs/lithium/util/Validator

    使用他们的类,你会得到这样的东西:

    // Input data. This can be an $object->data() or $_POST or whatever
    $data = array(
        'email' => 'someinvalidemailaddress';
    );
    
    // Validation rules
    $rules = array(
        'email' => array(
            array('notEmpty', 'message' => 'email is empty'),
            array('email', 'message' => 'email is not valid')
        )
    );
    
    // Perform validation
    Validator::check($data, $rules);
    
    // If this were in your object
    public validate($data = array(), $rules = array()) {
        $data = !empty($data) ? $data : $this->data; // Use whatever data is available
        $rules = $this->rules + $rules; // Merge $this's own rules with any passed rules
    
        return Validator::check($data, $rules));
    }
    
    // You can have a save method like
    public save() {
        if ($this->validates()) {
            // insert or update
        }
    }
    
    // And your object would 
    $user = new User();
    $user->data = array('email' => 'whatever');
    $user->save();
    

    而且总是有 Zend Validate。你可以在http://framework.zend.com/manual/en/zend.validate.set.html查看它

    【讨论】:

    • 向我解释了我应该如何实现我的课程,真的很喜欢 lithify 处理错误的方式。我将实施我自己的。谢谢.. BTW lithify.me 链接不起作用。
    【解决方案2】:

    首先创建您的验证类...然后在他们提交代码时。只需在表单上包含您在哪里设置的每个操作的类。您可以创建一个循环以通过验证输入的实例传递 POST 或 GET 数据。然后如果输入是好的,返回它(也许作为一个数组,这就是我所做的)并将它传递给你的数据库。

    例子:

    $validate = new validation_Class;     //new instance of the validation class
     $output = foreach($_POST as $input)         // loop each input data into the class
    {
       $validate->$input;
    }
    

    现在,如果您的验证类设置正确,您可以将所有干净的数据存储在 $output 中

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-12-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-10-19
      相关资源
      最近更新 更多