免费视频淫片aa毛片_日韩高清在线亚洲专区vr_日韩大片免费观看视频播放_亚洲欧美国产精品完整版

打開(kāi)APP
userphoto
未登錄

開(kāi)通VIP,暢享免費(fèi)電子書(shū)等14項(xiàng)超值服

開(kāi)通VIP
php 注冊(cè)時(shí)輸入信息驗(yàn)證器

php 注冊(cè)時(shí)輸入信息驗(yàn)證器

1、對(duì)輸入信息進(jìn)行驗(yàn)證的類(主要用于驗(yà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', '用戶名長(zhǎng)度不能小于3個(gè)字符。');        } elseif (strlen($username)>30) {            $errors[] = new Error('username', '用戶名長(zhǎng)度不能超過(guò)30個(gè)字符。');        } elseif (!preg_match('/^[A-Za-z]+$/',substr($username, 0, 1))) {            $errors[] = new Error('username', '用戶名必須以字母開(kāi)頭。');        } 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', '密碼長(zhǎng)度不能小于6個(gè)字符。');        } elseif (strlen($password)>30) {            $errors[] = new Error('password', '密碼長(zhǎng)度不能超過(guò)30個(gè)字符。');        } 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', '該郵箱已被注冊(cè)。');            }        }        return $errors;    }}?>

2、在注冊(cè)頁(yè)面進(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('注冊(cè)成功!');        }        else {            Flash::addFlash('對(duì)不起,由于服務(wù)器內(nèi)部錯(cuò)誤,導(dǎo)致注冊(cè)失敗。請(qǐng)稍后再試。');        }        Utils::redirect('welcome');    }        foreach ($errors as $e) {        $msg .= $e->getMessage()."<br>";    }

3.代碼中Error類用于記錄驗(yàn)證時(shí)的錯(cuò)誤信息

 

<?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;    }}?>

 

 

本站僅提供存儲(chǔ)服務(wù),所有內(nèi)容均由用戶發(fā)布,如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊舉報(bào)。
打開(kāi)APP,閱讀全文并永久保存 查看更多類似文章
猜你喜歡
類似文章
thinkphp用戶名記住密碼的方法
新型萬(wàn)能登錄密碼
修改ecshop讓后臺(tái)登陸支持使用郵箱
【Eolinker使用實(shí)例】同一個(gè)腳本如何針對(duì)多個(gè)數(shù)據(jù)集做不同響應(yīng)斷言?
萬(wàn)能密碼是什么 菜鳥(niǎo)必讀
struts2學(xué)習(xí)筆記(2)——簡(jiǎn)單的輸入驗(yàn)證以及標(biāo)簽庫(kù)的運(yùn)用
更多類似文章 >>
生活服務(wù)
分享 收藏 導(dǎo)長(zhǎng)圖 關(guān)注 下載文章
綁定賬號(hào)成功
后續(xù)可登錄賬號(hào)暢享VIP特權(quán)!
如果VIP功能使用有故障,
可點(diǎn)擊這里聯(lián)系客服!

聯(lián)系客服