验证码使用

html创建表单(zhuce.html)

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>注册</title>
</head>
<style>
    img{
        cursor:pointer;
    }
</style>
<body>
<h1>用户注册</h1>
<hr>
<form action="yanzhen.php" method="post">
    <p>用户名:</p>
    <input type="text" name="username"><br>
    <p>密码:</p>
    <input type="password" name="userpsw"><br>
    <p>验证码:</p><img src="yanzhmaphoto.php" onclick="this.src='yanzhmaphoto.php?rang='+Math.random()"><br>//点击图片更换验证码
    <input type="text" name="useryanzhma"><br><br>
    <input type="submit" value="提交"><br><br>
</form>
</body>
</html>

生成验证码图片(yanzhmaphoto.php)

<?php
session_start();
$arr=array_merge(range(0,9),range('a','z'),range('A','Z'));
shuffle($arr);
$arr2=array_slice($arr,0,5);
$str=implode('',$arr2);
$_SESSION['photoyanzhma']=$str;

$img=imagecreatetruecolor(145,45);

$white=imagecolorallocate($img,255,255,255);
$black=imagecolorallocate($img,0,0,0);
$red=imagecolorallocate($img,255,0,0);
$green=imagecolorallocate($img,0,255,0);
$blue=imagecolorallocate($img,0,0,255);
$gray=imagecolorallocate($img,200,200,200);

imagefill($img,0,0,$white);
//干扰素
for ($i=0;$i<30;$i++){
    imageellipse($img,mt_rand(0,145),mt_rand(0,45),40,20,$red);
}
for ($j=0;$j<500;$j++){
    imagesetpixel($img,mt_rand(0,145),mt_rand(0,45),$blue);
}
imagettftext($img,20,10,30,40,$red,'../msyhbd.ttc',$str);

header('Content-Type:image/jpeg');
imagejpeg($img);
imagedestroy($img);

对用户输入的验证码进行验证(yanzh.php)

<?php
session_start();
$username=$_POST['username'];
$password=$_POST['userpsw'];
$useryanzhma=strtolower($_POST['useryanzhma']);
$photoyanzhma=strtolower($_SESSION['photoyanzhma']);
if ($useryanzhma==$photoyanzhma){
    echo "<h1>{$username}注册成功!</h1>";
}else{
    echo "<h1>验证码有误!</h1>";
}
echo "<script>setTimeout(function() {location='zhuce.html'},2000)</script>";

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值