php 注册时输入信息验证器的实现详解
网络编程 2021-07-05 09:49www.168986.cn编程入门
本篇文章是对php中注册时输入信息验证器的实现方法进行了详细的分析介绍,需要的朋友参考下
1、对输入信息进行验证的类(主要用于验证用户名,密码,重复密码,邮箱,可添加其它功能)
<?php
/
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('wele');
}
foreach ($errors as $e) {
$msg .= $e->getMessage()."<br>";
}
3.代码中Error类用于记录验证时的错误信息
<?php
/
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
/
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('wele');
}
foreach ($errors as $e) {
$msg .= $e->getMessage()."<br>";
}
3.代码中Error类用于记录验证时的错误信息
代码如下:
<?php
/
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;
}
}
?>
编程语言
- 如何快速学会编程 如何快速学会ug编程
- 免费学编程的app 推荐12个免费学编程的好网站
- 电脑怎么编程:电脑怎么编程网咯游戏菜单图标
- 如何写代码新手教学 如何写代码新手教学手机
- 基础编程入门教程视频 基础编程入门教程视频华
- 编程演示:编程演示浦丰投针过程
- 乐高编程加盟 乐高积木编程加盟
- 跟我学plc编程 plc编程自学入门视频教程
- ug编程成航林总 ug编程实战视频
- 孩子学编程的好处和坏处
- 初学者学编程该从哪里开始 新手学编程从哪里入
- 慢走丝编程 慢走丝编程难学吗
- 国内十强少儿编程机构 中国少儿编程机构十强有
- 成人计算机速成培训班 成人计算机速成培训班办
- 孩子学编程网上课程哪家好 儿童学编程比较好的
- 代码编程教学入门软件 代码编程教程