当前位置:Gxlcms > PHP教程 > php验证码和不能正常显示解决办法

php验证码和不能正常显示解决办法

时间:2021-07-01 10:21:17 帮助过:44人阅读

php验证码和不能正常显示解决方法

效果如图:

?

文件都放到了同一文件夹下。

?

CreateImage.class.php

width=$width;	   $this->height=$height;	   $this->codenum=$codenum;	}		function outImg()	{	   //
输出头 $this->outFileHeader(); //产生验证码 $this->createCode(); //产生图片 $this->createImage(); //设置干扰像素 $this->setDisturbColor(); //往图片上写验证码 $this->writeCheckCodeToImage(); imagepng($this->checkimage); imagedestroy($this->checkimage); } private function outFileHeader() { header ('Content-type: image/png'); } private function createCode() { $this->checkcode = strtoupper(substr(md5(rand()),0,$this->codenum)); } private function createImage() { $this->checkimage = @imagecreate($this->width,$this->height); $back = imagecolorallocate($this->checkimage,255,255,255); $border = imagecolorallocate($this->checkimage,0,0,0); imagefilledrectangle($this->checkimage,0,0,$this->width - 1,$this->height - 1,$back); // 白色底 imagerectangle($this->checkimage,0,0,$this->width - 1,$this->height - 1,$border); // 黑色边框 } private function setDisturbColor() { for ($i=0;$i<=200;$i++) { $this->disturbColor = imagecolorallocate($this->checkimage, rand(0,255), rand(0,255), rand(0,255)); imagesetpixel($this->checkimage,rand(2,128),rand(2,38),$this->disturbColor); } } private function writeCheckCodeToImage() { for ($i=0;$i<=$this->codenum;$i++) { $bg_color = imagecolorallocate ($this->checkimage, rand(0,255), rand(0,128), rand(0,255)); $x = floor($this->width/$this->codenum)*$i; $y = rand(0,$this->height-15); imagechar ($this->checkimage, rand(5,8), $x, $y, $this->checkcode[$i], $bg_color); } } function __destruct() { unset($this->width,$this->height,$this->codenum); }}

?

checks.php

?

输出一个4位的随机数   $new_number.=dechex(rand(0,15));}$_SESSION[check_checks]=$new_number;    //将获取的随机数验证码写入到SESSION变量中     $num_image=imagecreate($image_width,$image_height);  //创建一个画布imagecolorallocate($num_image,255,255,255);     	 //设置画布的颜色for($i=0;$i输出字符}imagepng($num_image);      			//生成PNG格式的图像imagedestroy($num_image);  			//释放图像资源?>

?

index.php

?

rand函数的应用

?

************************************容易出现的问题*************

?

?

?

1.未开启php_gd2.dll? 去php.ini将其前面的“ ;”去掉

2.上面的checks.php中

3.字符编码改为统一的

4.最容易忽略的一点:

? 你本机上的环境是将报错提示全部打开的,所以在输出时有系统的未定义提示信息也跟着一起输出了,这样生成的图片肯定就不正常了,方法有两,在ValidationCode.php和checks.php文件开头加上ini_set('display_errors',?'Off');??就ok了

?

?

?

**********************************************************

?

人气教程排行