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