php注册时输入信息验证器的实现详解_php技巧
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了php注册时输入信息验证器的实现详解_php技巧,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含4597字,纯文字阅读大概需要7分钟。
内容图文
![php注册时输入信息验证器的实现详解_php技巧](/upload/InfoBanner/zyjiaocheng/262/5705a734c35b40b8a11a56effcabed10.jpg)
代码如下:
/**
* Validator for Register.
*/
final class RegisterValidator {
private function __construct() {
}
/**
* Validate the given username, password, repeat_password and email.
* @param $username, $password, $repeat_password and $email to be validated
* @return array array of {@link Error} s
*/
public static function validate($username, $password, $repeat_password, $email) {
$errors = array();
$username = trim($username);
$password = trim($password);
if (!$username) {
$errors[] = new Error('username', '用户名不能为空。');
} elseif (strlen($username)<3) {
$errors[] = new Error('username', '用户名长度不能小于3个字符。');
} elseif (strlen($username)>30) {
$errors[] = new Error('username', '用户名长度不能超过30个字符。');
} elseif (!preg_match('/^[A-Za-z]+$/',substr($username, 0, 1))) {
$errors[] = new Error('username', '用户名必须以字母开头。');
} elseif (!preg_match('/^[A-Za-z0-9_]+$/', $username)) {
$errors[] = new Error('username', '用户名只能是字母、数字以及下划线( _ )的组合。');
} elseif (!$password) {
$errors[] = new Error('password', '密码不能为空。');
} elseif (strlen($password)<6) {
$errors[] = new Error('password', '密码长度不能小于6个字符。');
} elseif (strlen($password)>30) {
$errors[] = new Error('password', '密码长度不能超过30个字符。');
} elseif (!preg_match('/^[A-Za-z0-9!@#\\$%\\^&\\*_]+$/', $password)) {
$errors[] = new Error('password', '密码只能是数字、字母或!@#$%^&*_等字符的组合。');
} elseif ($password != trim($repeat_password)) {
$errors[] = new Error('password', '两次输入密码不一致。');
} elseif (!Utils::isValidEmail($email)) {
$errors[] = new Error('email', '邮箱格式有误。');
} else {
// check whether user exists or not
$dao = new UserDao();
$user = $dao->findByName(trim($username));
if ($user) {
$errors[] = new Error('username', '该用户名已经被使用。');
}
$user = null;
// check whether email being used or not
$user = $dao->findByEmail(trim($email));
if ($user) {
$errors[] = new Error('email', '该邮箱已被注册。');
}
}
return $errors;
}
}
?>
2、在注册页面进行调用
代码如下:
$username = null;
$password = null;
$repeat_password = null;
$email = null;
$msg = "";
if (isset($_POST['username']) && isset($_POST['password'])
&& isset($_POST['repeat_password']) && isset($_POST['email'])) {
$username = addslashes(trim(stripslashes($_POST ['username'])));
$password = addslashes(trim(stripslashes($_POST ['password'])));
$repeat_password = addslashes(trim(stripslashes($_POST ['repeat_password'])));
$email = addslashes(trim(stripslashes($_POST ['email'])));
// validate
$errors = RegisterValidator::validate($username, $password, $repeat_password, $email);
// validate
if (empty($errors)) {
// save
$dao = new UserDao();
$user = new User();
$user->setEmail($email);
$last_login_ip = Utils::getIpAddress();
$user->setLastLoginIp($last_login_ip);
$user->setUsername($username);
$salt = substr(sha1(mt_rand()), 0, 22);
$hash_password = sha1($salt . $password);
$user->setPassword($hash_password);
$user->setSalt($salt);
$user = $dao->save($user);
if ($user) {
UserLogin::setUserInfo($user);
Flash::addFlash('注册成功!');
}
else {
Flash::addFlash('对不起,由于服务器内部错误,导致注册失败。请稍后再试。');
}
Utils::redirect('welcome');
}
foreach ($errors as $e) {
$msg .= $e->getMessage()."
";
}
3.代码中Error类用于记录验证时的错误信息
代码如下:
/**
* Validation error.
*/
final class Error {
private $source;
private $message;
/**
* Create new error.
* @param mixed $source source of the error
* @param string $message error message
*/
function __construct($source, $message) {
$this->source = $source;
$this->message = $message;
}
/**
* Get source of the error.
* @return mixed source of the error
*/
public function getSource() {
return $this->source;
}
/**
* Get error message.
* @return string error message
*/
public function getMessage() {
return $this->message;
}
}
?>
内容总结
以上是互联网集市为您收集整理的php注册时输入信息验证器的实现详解_php技巧全部内容,希望文章能够帮你解决php注册时输入信息验证器的实现详解_php技巧所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。