Blogger Information
Blog 49
fans 0
comment 0
visits 38125
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
php简单实现模拟用户登陆验证
超超多喝水
Original
1208 people have browsed it

为了保障用户账号密码安全,往往需要对用户的账号注册、密码等输入内容做一个检测限制,如弱口令限制,验证码防机器自动脚本限制等,下面对用户模拟注册做一个简单的限制:

实例

<!DOCTYPE html>
<html lang="zh-CN">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>用户注册页面</title>
    <style>
        #main {
            /*让div内部文字居中*/
            background-color: #fff;
            border-radius: 20px;
            width: 300px;
            height: 350px;
            margin: auto;
            position: absolute;
            top: 0;
            left: 0;
            right: 0;
            bottom: 0;
        }
    </style>
</head>

<body>
    <?php
    //显示除去 E_NOTICE 之外的所有错误信息
    error_reporting(E_ALL & ~E_NOTICE);
    //制作验证码
    //声明一个字符串集
    $codes = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890';
    //做一个四位的验证码,
    for($i=0;$i<4;$i++){
        $code .= "<span style='color:rgb(".mt_rand(0,255).",".mt_rand(0,255).",".mt_rand(0,255).")'>".$codes{mt_rand(0,strlen($codes)-1)}."</span>";
    } 
    ?>
    <div id="main">
        <!-- 建立html输入页面 -->
        <form action="" method="post">
            请输入账号:<input type="text" name="username" value="<?=$username?>" required><br>
            请输入密码:<input type="password" name="password1" required value="<?=$password1?>"><br>
            请确认密码:<input type="password" name="password2" required value="<?=$password2?>"><br>
            请输入验证码:<?=$code?>
                        <input type="text" name="code1"><br>
                        <input type="text" name="code2" value = "<?=$code?>" hidden><br>
                        <input type="submit" name="sub" value="注册">
        </form>
    </div>
    <?php
        //获取POST提交的内容
        $username = $_POST["username"];
        $password1 = $_POST["password1"];
        $password2 = $_POST["password2"];
        $sub = $_POST["sub"];
        $code1 = $_POST['code1'];
        //将验证码的内容转为小写
        $code2 = strip_tags($_POST['code2']);
        //将用户名首位转为大写
        $username = ucfirst($username);
        //数字连续字符弱口令判断
        $weak = strpos($password1,'012')===false?strpos($password1,'123')===false?strpos($password1,'234')===false?strpos($password1,'345')===false?strpos($password1,'456')===false?strpos($password1,'678')===false?strpos($password1,'789')===false?strpos($password1,'890')===false?:'':'':'':'':'':'':'':'';
        //检测是否有点击操作
        if(isset($sub)):
            //strcasecmp($string1,$string2)不区分大小写做判断,判断验证码输入是否正确
            if(strcasecmp($code1,$code2)!=0) $mess = "<span style='color:red;'>验证码输入错误</span>";
        endif;
        //如果没有报错且有点击操作往下执行
        if(!$mess && isset($sub)):
            //判断用户名首位是否是字母
            if(ord($username)<65 || ord($username)>90):    echo "<span style='color:red;'>用户名必须以字母开头</span>";
            else:
                //判断是否是弱口令
                if($weak!==false):  echo "<span style='color:red;'>密码过于简单,请重新输入</span>";
                else:
                    //判断两次输入密码是否一致
                    if($password1 !== $password2):  echo "<span style='color:red;'>两次输入密码不一致</span>";
                    else:
                        //判断密码长度是否在6-12位
                        if(strlen($password1)<6 || strlen($password1)>12): echo "<span style='color:red;'>密码长度请设置6-12位字符</span>";
                        //如果都符合条件返回注册成功提示并将密码加密方便后面传输
                        else:$password3 = sha1($password1);$password4 = sha1($password2);echo "注册成功,请返回登录";
                        endif;
                    endif;
                endif;
            endif;
        //如果有报错直接输出报错
        else:echo $mess;
        endif;
    ?>
</body>

</html>

运行实例 »

点击 "运行实例" 按钮查看在线实例


Correcting teacher:PHPzPHPz

Correction status:qualified

Teacher's comments:
Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments
Author's latest blog post
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!