欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  后端开发

一个高难度问题,关于验证码不显示

程序员文章站 2022-05-08 20:56:40
...
一般验证码不显示,无处乎是因为:1 有 BOM 头,2 extension=php_gd2.dll没有开启(即去掉分号)

但我这个不是上面这两个原因,因为我都检查了。
论坛里还有解决办法说是在页面中加上开头加上 ini_set('display_errors', 'Off');我也加也,(见这个帖子:http://bbs.csdn.net/topics/350011289)还是不行,
但说也奇怪,其他的cms程序却显示验证码,我用的php版本是PHP Version 5.3.28
要不我说这是高难度问题呢,这到底是怎么回事呢


回复讨论(解决方案)

不是明摆的吗?你把那五个错误纠正了就可以了

您要打开的页面,不应该是生成验证码的页面么?否则,你开了错误显示,又如何呢?

和个没有关系,那个是警告信息,和验证码无关,另 就是用了它error_reporting(E_ALL & ~E_NOTICE);验证码还是不显示。

贴出你的代码!

我不知道你是打算结决问题,还是在逗闷子

我建议不管你是啥,只要不喜欢我的问题的人请你离开我的帖子,世界很大,你感兴趣的地方很多,你给我多少钱我逗你玩呢?你就是给我钱我还考虑陪你玩不!

我不指望非得我的问题非得有人回答,我不指望非得有人对我的问题感兴趣!逗乐子,你不照照镜子,你是谁啊!你有这个资格吗

贴出你的代码!

我不知道你是打算结决问题,还是在逗闷子


从你的诸多回帖的方式来看,能判断出,你也就是php里面三四流角色,你不能解决的奇怪问题多着呢,别以为你认为很简单的问题就似乎不算问题,你差远去了!真正的高手,你看哪个象你这种嘴脸,你充其量是对php似懂非懂的人,我也是,但我和你不一样的地方是,我很谦虚。

不同情况不同分析。
你的截图只是说明验证码图片显示不出来,而上面的notice和warning是login.php的警告和提示,与验证码是没有任何关系的。
可以贴出你生成验证码图片的代码吗,这样才能分析。

不同情况不同分析。
你的截图只是说明验证码图片显示不出来,而上面的notice和warning是login.php的警告和提示,与验证码是没有任何关系的。
可以贴出你生成验证码图片的代码吗,这样才能分析。


说的没错,notice和warning是php使用中的一种警告,不影响代码运行,这个连刚学php的人都知道的常识。
还有,我的这个问题其实就是验证类中验证码部分的问题,起初我还以为是BOM头的问题,但是保存为无BOM头的也不行,另外,这段代码是一个教程中的范例,在主讲人的环境下是可以显示的,当然他用的php版本低。我的高些,但是高的兼容低的啊。
另外,GD也开启了,我也百度了不少方法没有奏效。所以才发此帖,关于代码很多,贴出来,估计大家也没有心情看。
还有,就是帖也要帖验证码那个php页,但是这个页我看了一下写的很规范。

验证码

font = ROOT_PATH.'/font/elephant.ttf';		}				//生成随机码		private function createCode() {			$_len = strlen($this->charset)-1;			for ($i=0;$icodelen;$i++) {				$this->code .= $this->charset[mt_rand(0,$_len)];			}		}				//生成背景		private function createBg() {			$this->img = imagecreatetruecolor($this->width, $this->height);			$color = imagecolorallocate($this->img, mt_rand(157,255), mt_rand(157,255), mt_rand(157,255));			imagefilledrectangle($this->img,0,$this->height,$this->width,0,$color);		}				//生成文字		private function createFont() {				$_x = $this->width / $this->codelen;			for ($i=0;$icodelen;$i++) {				$this->fontcolor = imagecolorallocate($this->img,mt_rand(0,156),mt_rand(0,156),mt_rand(0,156));				imagettftext($this->img,$this->fontsize,mt_rand(-30,30),$_x*$i+mt_rand(1,5),$this->height / 1.4,$this->fontcolor,$this->font,$this->code[$i]);			}		}				//生成线条、雪花		private function createLine() {			for ($i=0;$iimg,mt_rand(0,156),mt_rand(0,156),mt_rand(0,156));				imageline($this->img,mt_rand(0,$this->width),mt_rand(0,$this->height),mt_rand(0,$this->width),mt_rand(0,$this->height),$color);			}			for ($i=0;$iimg,mt_rand(200,255),mt_rand(200,255),mt_rand(200,255));				imagestring($this->img,mt_rand(1,5),mt_rand(0,$this->width),mt_rand(0,$this->height),'*',$color);			}		}				//输出		private function outPut() {			header('Content-type:image/png');			imagepng($this->img);			imagedestroy($this->img);		}				//对外生成		public function doimg() {			$this->createBg();			$this->createCode();			$this->createLine();			$this->createFont();			$this->outPut();		}				//获取验证码		public function getCode() {			return strtolower($this->code);		}			}

修改了一下,ok了,主要是字体那里,你没有定义ROOT_PATH,导致获取不到 $this->font = ROOT_PATH.'/font/elephant.ttf';
把ROOT_PATH用define方式改为正确路径就可以的,我现在测试目录是这样的。
test.php
font/elephant.ttf
test.php中的ROOT_PATH设置为 define('ROOT_PATH', dirname(__FILE__));

完整测试代码如下:

//验证码类define('ROOT_PATH', dirname(__FILE__));//验证码类class ValidateCode {    private $charset = 'abcdefghkmnprstuvwxyzABCDEFGHKMNPRSTUVWXYZ23456789';    //随机因子    private $code;                            //验证码    private $codelen = 4;                    //验证码长度    private $width = 130;                    //宽度    private $height = 50;                    //高度    private $img;                                //图形资源句柄    private $font;                                //指定的字体    private $fontsize = 20;                //指定字体大小    private $fontcolor;                        //指定字体颜色         //构造方法初始化    public function __construct() {        $this->font = ROOT_PATH.'/font/elephant.ttf';    }         //生成随机码    private function createCode() {        $_len = strlen($this->charset)-1;        for ($i=0;$icodelen;$i++) {            $this->code .= $this->charset[mt_rand(0,$_len)];        }    }         //生成背景    private function createBg() {        $this->img = imagecreatetruecolor($this->width, $this->height);        $color = imagecolorallocate($this->img, mt_rand(157,255), mt_rand(157,255), mt_rand(157,255));        imagefilledrectangle($this->img,0,$this->height,$this->width,0,$color);    }         //生成文字    private function createFont() {           $_x = $this->width / $this->codelen;        for ($i=0;$icodelen;$i++) {            $this->fontcolor = imagecolorallocate($this->img,mt_rand(0,156),mt_rand(0,156),mt_rand(0,156));            imagettftext($this->img,$this->fontsize,mt_rand(-30,30),$_x*$i+mt_rand(1,5),$this->height / 1.4,$this->fontcolor,$this->font,$this->code[$i]);        }    }         //生成线条、雪花    private function createLine() {        for ($i=0;$iimg,mt_rand(0,156),mt_rand(0,156),mt_rand(0,156));            imageline($this->img,mt_rand(0,$this->width),mt_rand(0,$this->height),mt_rand(0,$this->width),mt_rand(0,$this->height),$color);        }        for ($i=0;$iimg,mt_rand(200,255),mt_rand(200,255),mt_rand(200,255));            imagestring($this->img,mt_rand(1,5),mt_rand(0,$this->width),mt_rand(0,$this->height),'*',$color);        }    }         //输出    private function outPut() {        header('Content-type:image/png');        imagepng($this->img);        imagedestroy($this->img);    }         //对外生成    public function doimg() {        $this->createBg();        $this->createCode();        $this->createLine();        $this->createFont();        $this->outPut();    }         //获取验证码    public function getCode() {        return strtolower($this->code);    }     }$obj = new ValidateCode();$obj->doimg();


我明白了,我没有把这个源码放在根目录下造成,谢谢,功力深厚啊