php 注冊時輸入信息驗證器的實現(xiàn)詳解
更新時間:2013年07月05日 10:46:54 作者:
本篇文章是對php中注冊時輸入信息驗證器的實現(xiàn)方法進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下
1、對輸入信息進(jìn)行驗證的類(主要用于驗證用戶名,密碼,重復(fù)密碼,郵箱,可添加其它功能)
<?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', '用戶名只能是字母、數(shù)字以及下劃線( _ )的組合。');
} 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', '密碼只能是數(shù)字、字母或!@#$%^&*_等字符的組合。');
} 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', '該用戶名已經(jīng)被使用。');
}
$user = null;
// check whether email being used or not
$user = $dao->findByEmail(trim($email));
if ($user) {
$errors[] = new Error('email', '該郵箱已被注冊。');
}
}
return $errors;
}
}
?>
2、在注冊頁面進(jìn)行調(diào)用
$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('對不起,由于服務(wù)器內(nèi)部錯誤,導(dǎo)致注冊失敗。請稍后再試。');
}
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;
}
}
?>
復(fù)制代碼 代碼如下:
<?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', '用戶名只能是字母、數(shù)字以及下劃線( _ )的組合。');
} 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', '密碼只能是數(shù)字、字母或!@#$%^&*_等字符的組合。');
} 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', '該用戶名已經(jīng)被使用。');
}
$user = null;
// check whether email being used or not
$user = $dao->findByEmail(trim($email));
if ($user) {
$errors[] = new Error('email', '該郵箱已被注冊。');
}
}
return $errors;
}
}
?>
2、在注冊頁面進(jìn)行調(diào)用
復(fù)制代碼 代碼如下:
$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('對不起,由于服務(wù)器內(nèi)部錯誤,導(dǎo)致注冊失敗。請稍后再試。');
}
Utils::redirect('welcome');
}
foreach ($errors as $e) {
$msg .= $e->getMessage()."<br>";
}
3.代碼中Error類用于記錄驗證時的錯誤信息
復(fù)制代碼 代碼如下:
<?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+Ajax異步通訊實現(xiàn)用戶名郵箱驗證是否已注冊( 2種方法實現(xiàn))
- php發(fā)送短信驗證碼完成注冊功能
- 注冊頁面之前先驗證用戶名是否存在的php代碼
- php用戶注冊頁面利用js進(jìn)行表單驗證具體實例
- php用戶注冊信息驗證正則表達(dá)式
- 用Php編寫注冊后Email激活驗證的實例代碼
- php自動注冊登錄驗證機制實現(xiàn)代碼
- PHP學(xué)習(xí)筆記 用戶注冊模塊用戶類以及驗證碼類
- Thinkphp實現(xiàn)短信驗證注冊功能
- php+ajax注冊實時驗證功能
- js和php郵箱地址驗證的實現(xiàn)方法
- PHP實現(xiàn)的激活用戶注冊驗證郵箱功能示例
相關(guān)文章
理解PHP5中static和const關(guān)鍵字的區(qū)別
理解PHP5中static和const關(guān)鍵字的區(qū)別...2007-03-03
PHP基于閉包思想實現(xiàn)的BT(torrent)文件解析工具實例詳解
這篇文章主要介紹了PHP基于閉包思想實現(xiàn)的BT(torrent)文件解析工具,結(jié)合具體實例形式分析了php針對torrent文件的讀取與解析相關(guān)操作技巧,需要的朋友可以參考下2017-08-08
PHP實現(xiàn)的mysql操作類【MySQL與MySQLi方式】
這篇文章主要介紹了PHP實現(xiàn)的mysql操作類,結(jié)合實例形式分析了MySQL與MySQLi方式連接與操作MySQL數(shù)據(jù)庫的常用方法封裝與使用技巧,需要的朋友可以參考下2017-10-10

