PHP开发注册页面之验证码 登录

PHP开发注册页面之验证码

验证码也是用外面的PHP代码写出来的,详情可以访问我们的验证码制作程序,本章节所用到的验证码程序如下

创建yanzhengma.php文件,验证码程序

<?php
session_start();
Header("Content-type:image/PNG");
$im = imagecreate(60, 25);
$back = imagecolorallocate($im, 245, 245, 245);
imagefill($im, 0, 0, $back);
$vcodes = "";

for($i = 0; $i < 4; $i++){
    $font = imagecolorallocate($im, rand(100, 255), rand(0, 100), rand(100, 255));
    $authnum = rand(0, 9);
    $vcodes .= $authnum;
    imagestring($im, 5, 9 + $i * 10, 5, $authnum, $font);
}
$_SESSION['VCODE'] = $vcodes;
for($i=0;$i<200;$i++) {
    $randcolor = imagecolorallocate($im, rand(0, 255), rand(0, 255), rand(0, 255));
    imagesetpixel($im, rand()%60, rand()%25, $randcolor); //
}
imagepng($im);
imagedestroy($im);
?>

注意:在线上运行验证码程序会出现乱码,需要将在本地运行

怎么把我们的验证码加入到页面中去呢?

打开我们的验证码程序,发现验证码在网页的显示,就是一张图片,这样我们就可以<img>标签了,代码如下

<p>验 证 码:<input type="text" name="yzm" id="yzm">
    <img src="yanzhengma.php">

src:就是我们的验证码程序,如下程序不在同一级目录,需要加具体路径


这样我们就把验证码加入到了页面中,但是我们想一下,我们一般点击验证码,验证码就会刷新一下,这就需要我们的JS来实现了,只需要在<img>标签后面加入下面这句代码就可以了

<img src="yanzhengma.php" onClick="this.src='yanzhengma.php?nocache='+Math.random()" style="cursor:hand">

我们把验证码和我们之前做的页面代码融合在一起


完整代码如下

<!doctype html>
<html>
<head>
    <meta charset="utf-8">
    <title>PHP中文网</title>
    <style type="text/css">
        body{background-color: rgba(223, 255, 231, 0.28)
        }
        .container{
            border-radius: 25px;
            box-shadow: 0 0 20px #222;
            width: 380px;
            height: 400px;
            margin: 0 auto;
            margin-top: 200px;
            background-color: rgba(152, 242, 242, 0.23);
        }
        .right {
            position: relative;
            left: 40px;
            top: 20px;
        }
        input{
            width: 180px;
            height: 25px;
        }
        button{
            background-color: rgba(230, 228, 236, 0.93);
            border: none;
            color: #110c0f;
            padding: 10px 70px;
            text-align: center;
            display: inline-block;
            font-size: 16px;
            cursor: pointer;
            margin-top: 30px;
            margin-left: 50px;
        }
    </style>
</head>
<body>
<form action="" method="post">
    <div class="container">
        <div class="right">
            <h2>用户注册</h2>
            <p>用 户 名:<input type="text" name="name" id="name"></p>
            <p>密  码: <input type="password" name="pwd" id="pwd"></p>
            <p>确认密码: <input type="password" name="pwdconfirm" id="pwdconfirm"></p>
            <p>验 证 码:<input type="text" name="yzm" id="yzm">
                <img src="yanzhengma.php" onClick="this.src='yanzhengma.php?nocache='+Math.random()" style="cursor:hand"></p>
            <p><button type="submit" value="注册" >立即注册</button></p>
        </div>
    </div>
</form>
</body>
</html>

现在css样式也有了,验证码也有了,下一步就是需要对我们的内容验证了,比如用户名和密码没填写是不让用户提交的,两次输入的密码不一样也是不让提交的,这就要用我们的js来实现。下一节我们就为大家讲述怎么用JS来判断这些信息



下一节
<!doctype html> <html> <head> <meta charset="utf-8"> <title>PHP中文网</title> <style type="text/css"> body{background-color: rgba(223, 255, 231, 0.28) } .container{ border-radius: 25px; box-shadow: 0 0 20px #222; width: 380px; height: 400px; margin: 0 auto; margin-top: 200px; background-color: rgba(152, 242, 242, 0.23); } .right { position: relative; left: 40px; top: 20px; } input{ width: 180px; height: 25px; } button{ background-color: rgba(230, 228, 236, 0.93); border: none; color: #110c0f; padding: 10px 70px; text-align: center; display: inline-block; font-size: 16px; cursor: pointer; margin-top: 30px; margin-left: 50px; } </style> </head> <body> <form action="" method="post"> <div class="container"> <div class="right"> <h2>用户注册</h2> <p>用 户 名:<input type="text" name="name" id="name"></p> <p>密  码: <input type="password" name="pwd" id="pwd"></p> <p>确认密码: <input type="password" name="pwdconfirm" id="pwdconfirm"></p> <p>验 证 码:<input type="text" name="yzm" id="yzm"> <img src="yanzhengma.php" onClick="this.src='yanzhengma.php?nocache='+Math.random()" style="cursor:hand"></p> <p><button type="submit" value="注册" >立即注册</button></p> </div> </div> </form> </body> </html>
提交 重置代码
章节 评论 笔记 课件
  • 取消 回复 发送
  • 取消 发布笔记 发送