Home  >  Article  >  Backend Development  >  How to solve the problem that the verification code cannot be displayed in php

How to solve the problem that the verification code cannot be displayed in php

王林
王林Original
2020-11-03 17:41:593111browse

php解决验证码无法显示的问题的方法:1、检查php代码,确保代码全部正确;2、编辑php.ini配置文件,修改【extension = php_gd2.dll】;3、清除缓存区;4、重新启动apache服务器。

How to solve the problem that the verification code cannot be displayed in php

PHP解决验证码无法显示的方法:

(推荐教程:php视频教程

第一步:先确保代码没有写错

第二步:确保gd扩展库的开启

在php.ini在设置:extension=php_gd2.dll

第三步:清除缓存区

PHP关于缓存区的三个函数

ob_get_contents() - 返回输出缓冲区的内容

ob_flush() - 冲刷出(送出)输出缓冲区中的内容

ob_clean() - 清空(擦掉)输出缓冲区

ob_end_flush() - 冲刷出(送出)输出缓冲区内容并关闭缓冲

ob_end_clean() - 清空(擦除)缓冲区并关闭输出缓冲

flush() - 刷新输出缓冲

第四步:重新启动Apache服务器并再次运行

代码实现:

//Captcha.class.php文件名
codelen   = $codelen;
		$this->width     = $width;
		$this->height    = $height;
		$this->fontsize  = $fontsize;
		$this->fontfile  = ".\\Public\\Home\\Fonts\\msyh.ttc";
		$this->code      = $this->createCode();
		$this->img       = $this->createImg();
		$this->createBg();     //给画布添加背景色
		$this->createText();  //写入字符串
		$this->line();       //增加干扰线
		$this->outPut();    //输出图像
	}

	//私有的生成验证码随机字符串
	private function createCode()
	{
		//产生随机的字符串数组
		$arr_str = array_merge(range('a', 'z'),range('A', 'Z'),range(0,9));
		//打乱数组
		shuffle($arr_str);
		shuffle($arr_str);
		//从数组中随机指定个数下标
		$arr_index = array_rand($arr_str,$this->codelen);
		//循环下标数组,构建随机字符串
		$str ="";
		foreach ($arr_index as $i) {
			$str .=$arr_str[$i];
		}
		//将验证码字符串存入sess
		$_SESSION['captcha'] = $str;
		return $str;
	}

	//私有的创建一个空画布
	private function createImg()
	{
		return imagecreatetruecolor($this->width, $this->height);
	}

	//私有的分配画布背景色
	private function createBg()
	{	
		$bgcolor = imagecolorallocate($this->img, mt_rand(0,255), mt_rand(0,200), mt_rand(100,255));
		//绘制带背景的矩形
		imagefilledrectangle($this->img, 0, 0,$this->width,$this->height, $bgcolor);
	}

	//私有的写入验证码字符串
	private function createText()
	{	
		//给文本分配颜色
		$color = imagecolorallocate($this->img, mt_rand(100,255), mt_rand(0,200), mt_rand(100,200));
		//写入字符串
		imagettftext($this->img, $this->fontsize, -12, 10, 20, $color, $this->fontfile, $this->code);
		//imagestring($this->img, $this->fontsize, 8, 8, $this->code, $color);
	}
	//私有的输出图像
	private function line()
	{
		//增加干扰线
		for($n=1;$n<$this->codelen;$n++){
			$linecolor = imagecolorallocate($this->img, mt_rand(0,200), mt_rand(0,200), mt_rand(0,200));
			imageline($this->img,mt_rand(0,80), mt_rand(0,30), mt_rand(0,100), mt_rand(0,30), $linecolor);
		}
	}
	//公共的输出图像
	private function outPut()
	{	
		//声明输出的内容的类型
		header("content-type:image/png");
		//输出图像
		imagepng($this->img);
		//销毁图像资源
		imagedestroy($this->img);
	}

}

MVC设计模式(部分代码如下)

l




login





相关推荐:php培训

The above is the detailed content of How to solve the problem that the verification code cannot be displayed in php. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn