日韩天天综合网_野战两个奶头被亲到高潮_亚洲日韩欧美精品综合_av女人天堂污污污_视频一区**字幕无弹窗_国产亚洲欧美小视频_国内性爱精品在线免费视频_国产一级电影在线播放_日韩欧美内地福利_亚洲一二三不卡片区

php登錄實例代碼:用戶名與密碼驗證器_PHP教程

編輯Tag賺U幣
教程Tag:暫無Tag,歡迎添加,賺取U幣!

推薦:php類:注冊與自動加載
工程目錄如下: 1、將需要注冊的類放在一個數(shù)組中 ?phpfinal class Utils { private function __construct() { } public static function getClasses($pre_path = '/') { $classes = array( 'DBConfig' = $pre_path.'DBConfig/DBConfig.php', 'User' = $pre_path.'Mode

1、登錄時對用戶輸入的用戶名、密碼進行驗證

<?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', '用戶名長度不能小于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 (!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', '用戶名或密碼錯誤。');
                }
            } else {
                $errors[] = new Error('username', '用戶名不存在。');
            }
        }
        return $errors;
    }
}
 
?>

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

2、調(diào)用驗證器進行驗證

$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>";
    }


分享:php代碼:循環(huán)跳出問題
//php當(dāng)前循環(huán)為1,循環(huán)由里到外依次遞增,break默認(rèn)為1,例如跳出第2層循環(huán)for ($i=0;$i3;$i++){ foreach (array(1,2,3) as $val){ foreach (array(1,2,3) as $val){ echo "1層循環(huán)br/"; break 2; //跳出第2層循環(huán) } echo "2層循環(huán)br/"; } echo "3層循環(huán)br/";}//結(jié)果

來源:未知//所屬分類:PHP教程/更新時間:2012-12-17
相關(guān)PHP教程