动态校验
captcha.php
<?php
session_start();
//画布
$image = imagecreatetruecolor(100, 30);
$bgcolor = imagecolorallocate($image, 255, 255, 255);
imagefill($image, 0, 0, $bgcolor);
//随机数字
/*
for ($i=0; $i < 4; $i++) {
$fontsize = 10;
$fontcolor = imagecolorallocate($image, rand(0,120), rand(0,120), rand(0,120));
$fontcontent = rand(0,9);
$x = ($i*100/4) + rand(5,10);
$y = rand(5,10);
imagestring($image, $fontsize, $x, $y, $fontcontent, $fontcolor);
}
*/
$captch_code = '';
//数字字母组合
for ($i=0; $i <4; $i++) {
$fontsize = 8;
$fontcolor = imagecolorallocate($image, rand(0,80), rand(0,80), rand(0,80));
$data = 'abcdefghijkmnpqrstuvwxy3456789';
$fontcontent = substr($data, rand(0,strlen($data)),1);
$captch_code .= $fontcontent;
$x = ($i*100/4) + rand(5,10);
$y = rand(5,10);
imagestring($image, $fontsize, $x, $y, $fontcontent, $fontcolor);
}
$_SESSION['authcode'] = $captch_code;
//将captch_code保存到session的authcode中
//干扰点
for ($i=0; $i <300 ; $i++) {
$pointcolor = imagecolorallocate($image, rand(80,220), rand(80,220), rand(80,220));
imagesetpixel($image, rand(1,99), rand(1,29), $pointcolor);
}
//干扰线
for ($i=0; $i <3 ; $i++) {
$linecolor = imagecolorallocate($image, rand(120,220), rand(120,220), rand(120,220));
imageline($image, rand(1,99), rand(1,29), rand(1,99), rand(1,29), $linecolor);
# code...
}
header('content-type: image/png');
imagepng( $image );
//end
imagedestroy( $image );
?>
form.php
<?php
header('content-type:text/html;charset=utf-8');
if(isset($_REQUEST['authcode'])){
session_start();
if(strtolower($_REQUEST['authcode'])==$_SESSION['authcode']){
echo '<font color="#0000CC">输入正确</font>';
}else{
echo '<font color="#CC0000"> <b>输入错误</b> </font>';
}
exit();
}
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>确认验证码</title>
</head>
<body>
<form method="post" action="./form.php">
<p>验证码图片:<img id="captcha_img" border="1" src="./captcha.php?r=<?php echo rand();?>" width:100px; height:30px" />
<a href="javascript:void(0)" onclick="document.getElementById('captcha_img').src='./captcha.php?r='+Math.random()">换一个?</a>
<p>请输入图片中的内容:<input type="text" name="authcode" value="" /></p>
<p><input type="submit" value="提交" ></p>
</form>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>确认验证码</title>
</head>
<?php
if(isset($_REQUEST['authcode'])){
session_start();
if(strtolower($_REQUEST['authcode'])==$_SESSION['authcode']){
echo '<font color="#0000CC">输入正确</font>';
}else{
echo '<font color="#CC0000"><b>输入错误<b></font>';
}
exit();
}
?>
<!--php代码还是放在head和body之间方便-->
<!--基本没啥好说的这几个表单直接替换“captcha.php”这些关键位置就可以了,另外就是画布的长宽
另外这里:document.getElementById('captcha_cn').src='./captcha_cn.php?r='+Math.random()的拼接规则注意一下-->
<body>
<form method="post" action="./form.php">
验证码图片:<img id="captcha_cn" src="./captcha_cn.php?r=<?php echo rand();?>" width=200 height=60>
<a href="javascript:void(0)" onclick="document.getElementById('captcha_cn').src='./captcha_cn.php?r='+Math.random()">换一个?</a>
<p>请输入图片内容:<input type="text" name="authcode" value="" placeholder="请输入图片中的内容"/></p>
<p><input type="submit" value="提交" style="padding:6px 20px;"></p>
</form>
</body>
</html>
动图更换验证码通过javascript修改 src地址,通过改变传入的参数,从而刷新验证码 <a href = "javascript:void(0)" onclick = "document.getElementById('captcha_img').src = './captcha.php?r='+Math.random()">换一个?</a>