国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 開發(fā) > PHP > 正文

php登錄實(shí)例代碼:用戶名與密碼驗(yàn)證器

2024-05-04 21:53:05
字體:
供稿:網(wǎng)友

1、登錄時(shí)對(duì)用戶輸入的用戶名、密碼進(jìn)行驗(yàn)證

<?php /** * Validator for Login. */final class LoginValidator {     private function __construct() {             }     /**     * Validate the given username and password.     * @param $username and $password to be validated     * @return array array of {@link Error} s     */    public static function validate($username, $password) {        $errors = array();        $username = trim($username);        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)度不能超過30個(gè)字符。');        } 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 (!trim($password)) {            $errors[] = new Error('password', '密碼不能為空。');        } else {            // check whether use exists or not            $dao = new UserDao();            $user = $dao->findByName($username);             if ($user) {                if (!($user->getPassword() == sha1($user->getSalt() . $password))) {                    $errors[] = new Error('password', '用戶名或密碼錯(cuò)誤。');                }            } else {                $errors[] = new Error('username', '用戶名不存在。');            }        }        return $errors;    }} ?>

Error是自己寫的一個(gè)類:

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

2、調(diào)用驗(yàn)證器進(jìn)行驗(yàn)證

$username = null;$password = null; $msg = ""; if (isset($_POST['username']) && isset($_POST['password'])) {    $username = addslashes(trim(stripslashes($_POST ['username'])));    $password = addslashes(trim(stripslashes($_POST ['password'])));    // validate    $errors = LoginValidator::validate($username, $password);         if (empty($errors)) {        // save the latest ip or login time into database, then processing page forwarding        $dao = new UserDao();        $user = $dao->findByName($username);        $last_login_ip = Utils::getIpAddress();        $user->setLastLoginIp($last_login_ip);        $now = new DateTime();        $user->setLastLoginTime($now);        $dao->save($user);        UserLogin::setUserInfo($user);        Flash::addFlash('登錄成功!');        Utils::redirect('welcome');    }         foreach ($errors as $e) {        $msg .= $e->getMessage()."<br>";    }


發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 大洼县| 泽库县| 江口县| 莒南县| 东城区| 黎城县| 南和县| 海淀区| 龙山县| 平舆县| 同心县| 宁津县| 全州县| 万年县| 忻州市| 龙南县| 瓮安县| 镇远县| 龙川县| 东源县| 兖州市| 古蔺县| 崇左市| 通河县| 慈溪市| 任丘市| 商河县| 黄石市| 镇原县| 荥经县| 南投市| 南华县| 长沙市| 杂多县| 金乡县| 武平县| 平山县| 荥阳市| 兴和县| 镇坪县| 永善县|