php中文验证码
图形码类文件CnValidataCode.class.php
font='Images/font/SimSum.ttf'; //设置字体路径}//生成一个四位的随机码private function CheckCode(){$strdb = str_split($this->str,3); //str_split函数是将字符打散分配到数组,默认是1个字符形成1个数组元素,而汉字占3个字符,因此用3作为一个参数(此参数可有可无,默认为1),即1个汉字形成1个元素//$strdb数组for($i=0; $i<$this->codelen; $i++){$this->fontcolor = imagecolorallocate($this->img, mt_rand(0,120), mt_rand(0,120), mt_rand(0,120)); //随机分配字体颜色$code = $strdb[mt_rand(0,count($strdb)-1)]; //从数组$strdb 中随机选中1个元素(汉字)$this->captch_code .= $code;$_x = $this->width/$this->codelen; //一个汉字所占的宽度imagettftext($this->img,$this->fontsize,mt_rand(-10,10),$_x*$i,20,$this->fontcolor,$this->font,$code); //将文字渲染到画布(资源句柄)}}//创建一个图形验证码背景private function createBg(){$this->img = imagecreatetruecolor($this->width,$this->height); //新建一个真彩色图片赋值给图形验证码资源对象$color = imagecolorallocate($this->img,255,255,255); //为图形资源对象分配颜色,若失败,则返回-1 //上面的颜色也可以使用随机数,mt_rand(0,255),因本人喜欢简单的,就干脆用了纯白色的背景imagefilledrectangle($this->img,0,0,$this->width,$this->height,$color); //矩型填充,0,0代表左上角}//绘制干扰点和线private function createLine(){//干扰点 for($i=0; $i<200; $i++) { $pointcolor = imagecolorallocate($this->img, rand(50,200), rand(50,200), rand(50,200)); imagesetpixel($this->img, rand(1,$this->width-1), rand(1,$this->width-1), $pointcolor); } //干扰线 for($i=0; $i<10; $i++){ $linecolor = imagecolorallocate($this->img, rand(80,220), rand(80,220), rand(80,220)); imageline($this->img, rand(1,$this->width-1), rand(1,$this->width/3-1), rand(1,$this->width-1), rand(1,$this->width/3-1), $linecolor); } }private function doIput(){ob_clean(); header("Content-Type:image/png");imagepng($this->img);imagedestroy($this->img); }//获取图形验证码public function showImg(){$this->createBg();$this->CheckCode();$this->createLine();$this->doIput(); }//获取验证码的字符以便得到$_SESSION值public function getCode(){return $this->captch_code;}}
?>图形文件code.php
showImg();
$_SESSION['code']=$v->getCode();
?>表单文件index.html

验证文件check.php
本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!
