php 註冊時輸入資訊驗證器的實現詳解_PHP教程

來源:互聯網
上載者:User
1、對輸入資訊進行驗證的類(主要用於驗證使用者名稱,密碼,重複密碼,郵箱,可添加其它功能)
複製代碼 代碼如下:
/**
* 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;
}
}
?>

http://www.bkjia.com/PHPjc/327996.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/327996.htmlTechArticle1、對輸入資訊進行驗證的類(主要用於驗證使用者名稱,密碼,重複密碼,郵箱,可添加其它功能) 複製代碼 代碼如下: ?php /** * Validator for...

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.