失眠网,内容丰富有趣,生活中的好帮手!
失眠网 > PHP解决验证码无法显示的方法

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

时间:2022-04-08 07:23:50

相关推荐

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() - 刷新输出缓冲

当验证码图片无法显示时,在测试阶段(注释header(“content-type:image/png”)?,却能够显示图片的信息;在你找不到原因时:最有可能的是缓存区的问题;试着在运行的代码中添加ob_clean();

第四步:重新启动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('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设计模式编写的;其他代码无法完整上传;只提供部分运用的代码;

<?php //LoginController.class.php文件名//定义最终的登录控制器类final class LoginController{//创建验证码类对象public function captcha(){ob_clean();// 清空(擦掉)输出缓冲区//新建Captcha类对象;由于Captcha类只有一个公共的构造方法;新建对象时对调用构造方法里面传递的所有方法;$obj=new Captcha();}}

l<!-- login.htm 文件名 --><!DOCTYPE html><html lang="en"><head><meta charset="UTF-8"><title>login</title></head><body><img src="?p=Home&c=Login&a=captcha" onclick="this.src='?p=Home&c=Login&a=captcha&'+Math.random()" style="cursor: pointer;"><!-- onclick函数触发更新验证码 --></body></html>

如果觉得《PHP解决验证码无法显示的方法》对你有帮助,请点赞、收藏,并留下你的观点哦!

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。