分享一款比较好的php验证码程序,有需要的朋友参考一下,代码如下:
- <?php
-
-
-
-
-
-
-
-
-
- $num="";
- for($i=0;$i<4;$i++){
- $num .= rand(0,9);
- }
-
-
- Session_start();
- $_SESSION["Checknum"] = $num;
-
- Header("Content-type: image/PNG");
- srand((double)microtime()*1000000);
- $im = imagecreate(60,20);
- $black = ImageColorAllocate($im, 0,0,0);
- $gray = ImageColorAllocate($im, 200,200,200);
- imagefill($im,0,0,$gray);
-
-
- $style = array($black, $black, $black, $black, $black, $gray, $gray, $gray, $gray, $gray);
- imagesetstyle($im, $style);
- $y1=rand(0,20);
- $y2=rand(0,20);
- $y3=rand(0,20);
- $y4=rand(0,20);
- imageline($im, 0, $y1, 60, $y3, IMG_COLOR_STYLED);
- imageline($im, 0, $y2, 60, $y4, IMG_COLOR_STYLED);
-
-
- for($i=0;$i<80;$i++)
- {
- imagesetpixel($im, rand(0,60), rand(0,20), $black);
- }
-
- $strx=rand(3,8);
- for($i=0;$i<4;$i++){
- $strpos=rand(1,6);
- imagestring($im,5,$strx,$strpos, substr($num,$i,1), $black);
- $strx+=rand(8,12);
- }
- ImagePNG($im);
- ImageDestroy($im);
- ?>
使用方法:本程序可以直接运行,运行之后即可看到一个图形验证码,每次刷新都随机生成新码.
在某页面中使用此程序时,可以用以下代码:
- 请输入验证码:
- <input type=text name=passcode>
- <img src=showimg.php>
这样即可显示出验证码图片,到了验证页面,用以下代码:
- $code=$_POST["passcode"];
- if( $code == $_SESSION["Checknum"]){
- 验证通过
- }else{
- 验证码错误
- }
|