【发布时间】: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();
}
}
现在是我专注于改进验证代码的时候了。我希望我的所有类方法在插入数据库之前验证用户输入。基本上一个接受用户输入的类方法应该能够执行以下操作。
如果类方法接受用户输入作为数组,则检查数组中的所有必需输入。
检查输入的最小和最大长度。
检查任何非法字符。
等等
我想知道如何处理这个问题,以及是否有任何 PHP 独立验证组件可以帮助我。这会有很大帮助。如果我做错了,请建议我改进我的代码,只要它保证我的代码遵循编码标准,我不介意做任何事情。
如果有人能向我解释处理验证逻辑以验证对象的类方法的用户输入,我将不胜感激。
谢谢你..
【问题讨论】:
标签: php validation