PHP头条
热点:

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


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

?

?

index.php

?





rand函数的应用



     
 
     
  用户名  
  密?码  
  验证码     
        
 
     

?

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

?

?

?

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

2.上面的checks.php中

3.字符编码改为统一的

4.最容易忽略的一点:

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

?

?

?

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

?

www.phpzy.comtrue/phprm/5334.htmlTechArticlephp验证码和不能正常显示解决办法 php验证码和不能正常显示解决方法 效果如图: ? 文件都放到了同一文件夹下。 ? CreateImage.class.php width=$width; $this->height=$height; $this->codenum=$codenum;}func...

相关文章

相关频道:

PHP之友评论

今天推荐