熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> PHP編程 >> 正文

php 注冊時輸入信息驗證器的實現詳解

2013-11-15 12:27:57  來源: PHP編程 
本篇文章是對php中注冊時輸入信息驗證器的實現方法進行了詳細的分析介紹需要的朋友參考下  

  對輸入信息進行驗證的類(主要用於驗證用戶名密碼重復密碼郵箱可添加其它功能)

復制代碼 代碼如下:
<?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)<) {
            $errors[] = new Error(username 用戶名長度不能小於個字符);
        } elseif (strlen($username)>) {
            $errors[] = new Error(username 用戶名長度不能超過個字符);
        } elseif (!preg_match(/^[AZaz]+$/substr($username ))) {
            $errors[] = new Error(username 用戶名必須以字母開頭);
        } elseif (!preg_match(/^[AZaz_]+$/ $username)) {
            $errors[] = new Error(username 用戶名只能是字母數字以及下劃線( _ )的組合);
        } elseif (!$password) {
            $errors[] = new Error(password 密碼不能為空);
        } elseif (strlen($password)<) {
            $errors[] = new Error(password 密碼長度不能小於個字符);
        } elseif (strlen($password)>) {
            $errors[] = new Error(password 密碼長度不能超過個字符);
        } elseif (!preg_match(/^[AZaz!@#$%^&*_]+$/ $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;
    }
}
?>

  
在注冊頁面進行調用

復制代碼 代碼如下:
$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(sha(mt_rand()) );
        $hash_password = sha($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>";
    }

  
代碼中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;
    }
}
?>


From:http://tw.wingwit.com/Article/program/PHP/201311/21003.html
    推薦文章
    Copyright © 2005-2013 電腦知識網 Computer Knowledge   All rights reserved.