无为清净楼资源网 Design By www.qnjia.com
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('welcome');
}
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('welcome');
}
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,注册,信息验证器
无为清净楼资源网 Design By www.qnjia.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
无为清净楼资源网 Design By www.qnjia.com
暂无评论...
更新日志
2024年09月23日
2024年09月23日
- 华少翌.2005-华少翌【中唱艺能】【WAV+CUE】
- 群星《十二女声Ⅱ》[WAV/CUE/分轨][527MB]
- 大自然音乐系列《漫步在山裡》[WAV/CUE/分轨][502.8MB]
- 大自然音乐系列《星光夜语》[WAV/CUE/分轨][453.3MB]
- 史依弘《史依弘交响乐京剧演唱专辑HQ》头版限量编号[WAV+CUE]
- 曼丽2020《女人三十风继续吹》HQCD[WAV+CUE]
- 曼丽2021《女人三十2雾之恋》HQCD[WAV+CUE]
- 隔壁老樊.2019-我曾【好听音乐】【FLAC分轨】
- 高林生.1993-牵挂你的人是我【白天鹅】【WAV+CUE】
- 洪卓立.2012-找个懂我的女孩(国专)【英皇娱乐】【WAV+CUE】
- 曼丽2022《女人三十3晚秋》HQCD[WAV+CUE]
- 曼丽2018《女人三十4冬天》HQCD[WAV+CUE]
- 柏菲-《好歌30年壹》限量开盘母带ORMCD[低速原抓WAV+CUE]
- 大自然音乐系列《水舞》[WAV/CUE/分轨][424.3MB]
- 大自然音乐系列《星光夜语》[WAV/CUE/分轨][453.3MB]