一个实用的php验证码类
万能php验证码类,供大家参考,具体内容如下
code.php是验证码类,类的名称最好和文件名的名称一样,这样有利于我们的查看。
code.php
number=$number; $this->codeType=$codeType; $this->width=$width; $this->height=$height; //生成验证码函数 $this->code=$this->createCode(); } publicfunction__get($name){ if($name=='code'){ return$this->code; } returnfalse; } /*获取验证码*/ publicfunctiongetCode(){ return$this->code; } /*图像资源销毁*/ publicfunction__destruct(){ imagedestroy($this->image); } protectedfunctioncreateCode(){ //通过你的验证码类型生成验证码 switch($this->codeType){ case0://纯数字 $code=$this->getNumberCode(); break; case1://纯字母的 $code=$this->getCharCode(); break; case2://数字和字母混合 $code=$this->getNumCharCode(); break; default: die('不支持此类验证码类型'); } return$code; } protectedfunctiongetNumberCode(){ $str=join('',range(0,9)); returnsubstr(str_shuffle($str),0,$this->number); } protectedfunctiongetCharCode(){ $str=join('',range('a','z')); $str=$str.strtoupper($str); returnsubstr(str_shuffle($str),0,$this->number); } protectedfunctiongetNumCharCode(){ $numstr=join('',range(0,9)); $str=join('',range('a','z')); $str=$numstr.$str.strtoupper($str); returnsubstr(str_shuffle($str),0,$this->number); } protectedfunctioncreateImage(){ $this->image=imagecreatetruecolor($this->width, $this->height); } protectedfunctionfillBack(){ imagefill($this->image,0,0,$this->lightColor()); } /*浅色*/ protectedfunctionlightColor(){ returnimagecolorallocate($this->image,mt_rand(133,255),mt_rand(133,255),mt_rand(133,255)); } /*深色*/ protectedfunctiondarkColor(){ returnimagecolorallocate($this->image,mt_rand(0,120),mt_rand(0,120),mt_rand(0,120)); } protectedfunctiondrawChar(){ $width=ceil($this->width/$this->number); for($i=0;$i<$this->number;$i++){ $x=mt_rand($i*$width+5,($i+1)*$width-10); $y=mt_rand(0,$this->height-15); imagechar($this->image,5,$x,$y,$this->code[$i],$this->darkColor()); } } protectedfunctiondrawLine(){ for($i=0;$i<5;$i++){ imageline($this->image,mt_rand(0,$this->width),mt_rand(0,$this->height),mt_rand(0,$this->width),mt_rand(0,$this->height),$this->darkColor()); } } protectedfunctiondrawDisturb(){ for($i=0;$i<150;$i++){ $x=mt_rand(0,$this->width); $y=mt_rand(0,$this->height); imagesetpixel($this->image,$x,$y,$this->lightColor()); } } protectedfunctionshow(){ header('Content-Type:image/png'); imagepng($this->image); } publicfunctionoutImage(){ //创建画布 $this->createImage(); //填充背景色 $this->fillBack(); //将验证码字符串花到画布上 $this->drawChar(); //添加干扰元素 $this->drawDisturb(); //添加线条 $this->drawLine(); //输出并显示 $this->show(); } }
http://dh.wk163.comtest.php是new一个新的验证码,并把它保存到session中,为我们验证码的验证起到保存和存储的作用。
http://dh.wk163.comtest.php
getCode(); $code->outImage();
login.php就是最后的验证。
login.php
alert('验证码正确!');"; }else{ echo"alert('验证码错误!');"; } } ?> *{margin:0px;padding:0px;} ul{ width:400px; list-style:none; margin:50pxauto; } li{ padding:12px; position:relative; } label{ width:80px; display:inline-block; float:left; line-height:30px; } input[type='text'],input[type='password']{ height:30px; } img{ margin-left:10px; } input[type="submit"]{ margin-left:80px; padding:5px10px; }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持毛票票。