PHP - 验证码制作加验证

一,主页 index.php  

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<body>
<script type="text/javascript">
function refresh() {
document.getElementById("codeP").src = "picture.php?tm="+Math.random();
}
</script>
<form action="check.php" method="post">
<div>
<img id="codeP" src="picture.php" onclick="refresh()"/>
<input id="codeT" type="text" name="codeT" />
</div>
</form>
</body>
</html>
 
二,生成验证码 picture.php
<?php

header("Cache-Control: no-cache, must-revalidate");
// 声明图像大小
$img_height=70;
$img_width=25;
$authnum='';
// 验证码内容
$ychar="0,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z";
$list=explode(",",$ychar);
for($i=0;$i<4;$i++){
$randnum=rand(0,35);
$authnum.=$list[$randnum];
}
// session有效
session_start();

// 将每次生成的验证码保存在session中
$_SESSION["sessionCode"] = $authnum;
// 生成一个基本大小图像
$aimg = imagecreate($img_height,$img_width);
// 图像填充颜色
imagecolorallocate($aimg, 255,255,255);
$black = imagecolorallocate($aimg, 0,0,0);
 
for ($i=1; $i<=100; $i++) { imagestring($aimg,1,mt_rand(1,$img_height),mt_rand(1,$img_width),"@",imagecolorallocate($aimg,mt_rand(200,255),mt_rand(200,255),mt_rand(200,255)));
}
 
//为了区别于背景,这里的颜色不超过200,上面的不小于200
for ($i=0;$i<strlen($authnum);$i++){
imagestring($aimg, mt_rand(3,5),$i*$img_height/4+mt_rand(2,7),mt_rand(1,$img_width/2-2), $authnum[$i],imagecolorallocate($aimg,mt_rand(0,100),mt_rand(0,150),mt_rand(0,200)));
}
imagerectangle($aimg,0,0,$img_height-1,$img_width-1,$black);//画一个矩形
Header("Content-type: image/PNG");
ImagePNG($aimg); //生成png格式
ImageDestroy($aimg);
?>
 
三,接收测试 验证码 check.php
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<body>
<?php
session_start();
$codeT = $_POST['codeT'];
$codeP = $_SESSION["sessionCode"];
echo '输入:'.$codeT.'<br>';
echo '验证码'.$codeP.'<br>';
if ($codeT==$codeP) {
echo '正确<br>';
} else {
echo '错误';
}?>
</body>
</html>
posted @ 2019-07-20 18:13  武卡卡  阅读(442)  评论(0编辑  收藏  举报