A-A+

PHP功能很强大的验证码

2012年06月22日 PHP技术文章 评论 2 条 阅读 243 views 次

一个php的验证码,并且还带有验证码判断文件,利用cookie判断验证码是否正确,验证码基础原理还是利用图像生成和干扰字符组合,这款验证码主要强大就在于利用了php数组进行干扰字符的组合,干扰了好几次,看来注册机器不好识别原理了,php验证码方法文件 yzm.php,php验证码使用判断文件:yzmdy.php,php验证码JS点击刷新文件:js.js,下面看看这三个文件的代码,使用时将其复制到同一个目录即可。

php验证码方法文件:yzm.php

<?php   
$im=imagecreate(100,40);   
$bgcolor=imagecolorallocate($im,0,0,0);//生成背景颜色   
$fontcolor=imagecolorallocate($im,0,0,255);//生成线条颜色   
$te=imagecolorallocate($im,255,255,0);//生成点的颜色   
$wz=imagecolorallocate($im,rand(0,255),rand(0,255),rand(0,255));   
$wz1=imagecolorallocate($im,rand(0,255),rand(0,255),rand(0,255));   
$wz2=imagecolorallocate($im,rand(0,255),rand(0,255),rand(0,255));   
$wz3=imagecolorallocate($im,rand(0,255),rand(0,255),rand(0,255));   
for($x=1;$x<=10;$x++){   
imageline($im,rand(0,100),rand(0,40),rand(0,100),rand(20,40),$fon
tcolor);//画线   
}   
for($d=0;$d<200;$d++){   
imagesetpixel($im,rand()%100,rand()%100,$te);   
}   
$arr=array(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);   
  
$str1=$arr&#91;rand(0,36)&#93;;   
imagestring($im,6,20,20,$str1,$wz);//写字   
$str2=$arr&#91;rand(0,36)&#93;;   
imagestring($im,6,30,20,$str2,$wz1);//写字   
$str3=$arr&#91;rand(0,36)&#93;;   
imagestring($im,6,40,20,$str3,$wz2);//写字   
$str4=$arr&#91;rand(0,36)&#93;;   
$str=$str1.$str2.$str3.$str4;   
setcookie('yzm',$str);   
imagestring($im,6,50,20,$str4,$wz3);//写字   
header("content-type:image/jpeg");   
imagejpeg($im);   
?>

php验证码使用文件:

<?php   
if($_POST&#91;'sub'&#93;){   
 if($_COOKIE&#91;'yzm'&#93;==$_POST&#91;'yzm'&#93;){   
  echo "正确";   
 }   
 else{   
  echo "错误";   
 }   
}?>   
<script language="javascript" src="js.js"></script>   
<form action="" method="post" enctype="multipart/form-data">   
请输入验证码:<input type="text" name="yzm"/><br>   
<img src="yzm.php" id="img" onclick="iga();"/>   
<input type="submit" name="sub" value="提交"/>   
</form>

php验证码点击刷新JS文件:

[cc lang="Javascript"]
function iga(){
document.getElementById('img').src='yzm.php?'+Math.random();
}
[/code]

标签:

2 条留言  访客:2 条  博主:0 条

  1. 华信

    Code写的不错, 但你怎么界定多少的rand()背景干扰是最合适的呢?

    • smiling

      只要验证码文字能看得清楚就行。。

给我留言