PHP简单验证码类


PHP #验证码 #2012-04-30 09:26

用法:

1$captcha = new Captcha(array('type'=>0, 'length'=>4));
2 
3$m = isset($_GET['m'])?trim($_GET['m']):'';
4 
5yige_setcookie($m.'verify', md5(strtoupper($captcha->verify_str)), 0);
6 
7$captcha->draw();

效果图:


以下是类代码:

01<?php
02/* 一个网在线教程 http://yige.org */
03 
04class Captcha {
05     
06    var $verify_str = "";//随机产生的字符
07    var $verify_width = 0;//验证图片宽度
08    var $verify_height = 22;//验证图片高度
09    var $char_width = 12;//字符宽度
10    var $type = 0;//字符类型
11    var $length = 4;//字符长度
12     
13    function Captcha($config = array()) {
14        if (count($config) > 0) {
15            $this->init($config);
16        }
17        $this->verify_width = $this->length * $this->char_width;
18         
19        $this->init_verify_str();
20    }
21     
22    function init($config) {
23        foreach ($config as $key => $val) {
24            if (isset($this->$key)) {
25                $this->$key = $val;
26            }
27        }
28    }
29     
30    function init_verify_str() {
31        switch ($this->type) {
32            case 0:
33                $seed = array ('0','1','2','3','4','5','6','7','8','9');
34                break;
35            case 1:
36                $seed = array ('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');
37                break;
38            default:
39                $seed = 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');
40        }
41         
42        shuffle($seed);
43        $verify_arr = array_slice($seed, 0, $this->length);
44        $this->verify_str = implode("", $verify_arr);
45    }
46     
47    function draw() {
48        header("Pragma:no-cache");
49        header("Cache-control:no-cache");
50        header("Content-type: image/png");
51         
52        $aimg = imagecreate($this->verify_width, $this->verify_height);
53        $back = imagecolorallocate($aimg, 251, 254, 255);
54        imagefilledrectangle($aimg, 0, 0, $this->verify_width - 1, $this->verify_height - 1, $back);
55        //$border = imagecolorallocate($aimg, 133, 153, 193);
56        //imagerectangle($aimg, 0, 0, $this->verify_width - 1, $this->verify_height - 1, $border);
57         
58        for ($i = 0; $i < $this->length; $i++) {
59            $words = imagecolorallocate($aimg, rand(20,200), rand(20,200), rand(20,200));
60            $wxp = 5+$i*10+rand(1,3);
61            imageString($aimg, 5, $wxp, 1+rand(0,3), substr($this->verify_str,$i,1),$words);
62        }
63         
64        $pixcol = imagecolorallocate($aimg, rand(0,255), rand(0,255), rand(0,255));
65        for ($i=0; $i<20; $i++) {
66            imagesetpixel($aimg,rand(2,$this->verify_width-2),rand(2,$this->verify_height-2),$pixcol);
67        }
68         
69        imagepng($aimg);
70        imagedestroy($aimg);
71    }
72     
73}
74?>


相关文章

粤ICP备11097351号-1