栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > PHP

php 注册时输入信息验证器的实现详解

PHP 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

php 注册时输入信息验证器的实现详解

1、对输入信息进行验证的类(主要用于验证用户名,密码,重复密码,邮箱,可添加其它功能)
复制代码 代码如下:

final class RegisterValidator {
    private function __construct() {

    }
   
    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类用于记录验证时的错误信息
复制代码 代码如下:

final class Error {
    private $source;
    private $message;
   
    function __construct($source, $message) {
        $this->source = $source;
        $this->message = $message;
    }
   
    public function getSource() {
        return $this->source;
    }
   
    public function getMessage() {
        return $this->message;
    }
}
?>
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/49407.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号