php验证码类,超级好用,下面贴一下程序的代码,程序里有详细的注释,方便大家研究.
注:此类需要gd库支持
下载址址: php验证码类
效果图:
例子demo:
以下为引用的内容: <body> |
调用文件vcode.php:
以下为引用的内容: <?php /** * 默认验证码session为vcode.即:$_SESSION['vcode']; * 注意在给变量符值时不要把变量的名子和SESSION冲突 * 注:在验证时不分大小写 */ include("inc_vcode_class.php"); $v = new vcode; //$config['width'] = 50; //验证码宽 //$config['height'] = 20; //验证码高 //$config['vcode'] = "vcode"; //检查验证码时用的SESSION //$config['type'] = "default"; //验证码展示的类型default:大写字母,string:小写字母,int:数字 //$config['length'] = 4; //验证码长度 //$config['interfere']= 10; //干扰线强度,范围为1-30,0或空为不起用干扰线 //$v->init($config); //配置 $v->create(); ?> |
验证码类inc_vcode_class.php:
以下为引用的内容: <?php /** * 验证码类 * 注:需要GD库支持 */ session_start(); class vcode{ private $config; private $im; private $str; function __construct(){ $this->config['width'] = 50; $this->config['height'] = 20; $this->config['vcode'] = "vcode"; $this->config['type'] = "default"; $this->config['length'] = 4; $this->config['interfere'] = 10; $this->str['default'] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; $this->str['string'] = "abcdefghijklmnopqrstuvwxyz"; $this->str['int'] = "0123456789"; } //配置 public function init($config=array()){ if (!empty($config) && is_array($config)){ foreach($config as $key=>$value){ $this->config[$key] = $value; } } } //输出验证码 public function create(){ if (!function_exists("imagecreate")){ return false; } $this->create_do(); } //创建 private function create_do(){ $this->im = imagecreate($this->config['width'],$this->config['height']); //设置背景为白色 imagecolorallocate($this->im, 255, 255, 255); //为此背景加个边框 $bordercolor= imagecolorallocate($this->im,37,37,37); imagerectangle($this->im,0,0,$this->config['width']-1,$this->config['height']-1,$bordercolor); //生成验证码 $this->create_str(); $vcode = $_SESSION[$this->config['vcode']]; //输入文字 $fontcolor = imagecolorallocate($this->im,46,46,46); for($i=0;$i<$this->config['length'];$i++){ imagestring($this->im,5,$i*10+6,rand(2,5),$vcode[$i],$fontcolor); } //加入干扰线 $interfere = $this->config['interfere']; $interfere = $interfere>30?"30":$interfere; if (!empty($interfere) && $interfere>1){ for($i=1;$i<$interfere;$i++){ $linecolor = imagecolorallocate($this->im,rand(0,255),rand(0,255),rand(0,255)); $x = rand(1,$this->config['width']); $y = rand(1,$this->config['height']); $x2 = rand($x-10,$x+10); $y2 = rand($y-10,$y+10); imageline($this->im,$x,$y,$x2,$y2,$linecolor); } } header("Pragma:no-cache\r\n"); header("Cache-Control:no-cache\r\n"); header("Expires:0\r\n"); header("content-type:image/jpeg\r\n"); imagejpeg($this->im); imagedestroy($this->im); exit; } //得到验证码 private function create_str(){ if ($this->config['type']=="int"){ for($i=1;$i<=$this->config['length'];$i++){ $vcode .= rand(0,9); } $_SESSION[$this->config['vcode']] = $vcode; return true; } $len = strlen($this->str[$this->config['type']]); if (!$len){ $this->config['type'] = "default"; $this->create_str(); } for($i=1;$i<=$this->config['length'];$i++){ $offset = rand(0,$len-1); $vcode .= substr($this->str[$this->config['type']],$offset,1); } $_SESSION[$this->config['vcode']] = $vcode; return true; } } ?> |
下载址址: php验证码类