Maison  >  Article  >  développement back-end  >  Comment résoudre le problème que le code de vérification ne peut pas être affiché en php

Comment résoudre le problème que le code de vérification ne peut pas être affiché en php

王林
王林original
2020-11-03 17:41:593210parcourir

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

Comment résoudre le problème que le code de vérification ne peut pas être affiché en 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文件名
<?php 

//定义最终的图像验证码类
final class Captcha{
	//私有的成员属性
	private $code;//验证码字符串
	private $codelen;//验证码长度
	private $width;  //画布宽度
	private $height;  //画布高度
	private $img;   //图像资源
	private $fontsize; //字号大小
	private $fontfile; //字体文件

	//构造方法:对象初始化
	public function __construct($codelen=4,$width=100,$height=36,$fontsize=20)
	{	
		$this->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(&#39;a&#39;, &#39;z&#39;),range(&#39;A&#39;, &#39;Z&#39;),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[&#39;captcha&#39;] = $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设计模式(部分代码如下)

<?php 
//LoginController.class.php文件名
//定义最终的登录控制器类
final class LoginController{
//创建验证码类对象
	public function captcha(){
		ob_clean();// 清空(擦掉)输出缓冲区
	
		//新建Captcha类对象;由于Captcha类只有一个公共的构造方法;新建对象时对调用构造方法里面传递的所有方法;
			$obj=new Captcha();
	}
}
l<!-- login.htm 文件名 -->
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>login</title>
</head>
<body>
<img src="?p=Home&c=Login&a=captcha" onclick="this.src=&#39;?p=Home&c=Login&a=captcha&&#39;+Math.random()" style="cursor: pointer;">
<!-- onclick函数触发更新验证码 -->
</body>
</html>

相关推荐:php培训

Ce qui précède est le contenu détaillé de. pour plus d'informations, suivez d'autres articles connexes sur le site Web de PHP en chinois!

Déclaration:
Le contenu de cet article est volontairement contribué par les internautes et les droits d'auteur appartiennent à l'auteur original. Ce site n'assume aucune responsabilité légale correspondante. Si vous trouvez un contenu suspecté de plagiat ou de contrefaçon, veuillez contacter admin@php.cn