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

一个超难的问题,关于验证码不显示

程序员文章站 2022-05-16 11:47:43
...
一般验证码不显示有下面几种情况
1、GD库没有开启,2、用记事本打开文件了,结果有BOM头
3、 的前面有空格了
但是我的验证码不显示和上述三种都没有关系,因为我检查了

但还是不显示,前提是我的这个验证码类写是没有问题,实例化也没有问题
但还是不显示,这到底为啥呢,想破头都没有想出来

我这个验证码加载的是一个字库elephant.ttf文件,是不是他的问题呢,如果是怎么解决呢


回复讨论(解决方案)

那就是你验证码文件引入路径错了

那就是你验证码文件引入路径错了


路么没错啊,我是这样引入的
//网站根目录define('ROOT_PATH',dirname(__FILE__));public function __construct() {			$this->font = ROOT_PATH.'/font/elephant.ttf';		}

或者说这个ttf文件不支持win8系统吗,因我的本地环境是win8 64位的

算了,把这个验证码类发上来吧

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);		}			}	?>

这一问题有几种可能
第一,这个字体文件有问题,即兼容性上的问题,因为我的系统是win8 64位的,我确定是否字体对于操作系统也有兼容性这一说;
第二,路径问题,但是即上面所罗列的设置,应该没有问题吧,常规的写法都是这么写啊。
至于啥GD库开启啥的,这些,都排除了,还有啥BOM头的,也排除了,那是啥原因呢

看这是我的本地环境中的GD库开启,我说的对吧

难道说还是引入 验证码这个文件的路径出错了吗?


字体 elephant 的文件名为 elephnt.ttf 斜体的是 elephnti.ttf 请认真核实
判断是否是字体文件造成的,可以用浏览器直接运行验证码程序


字体 elephant 的文件名为 elephnt.ttf 斜体的是 elephnti.ttf 请认真核实
判断是否是字体文件造成的,可以用浏览器直接运行验证码程序

不是它的原因,再说了,字体的文件名可以任意命名,只要真实的文件名和写在php页面中的文件中一致就可,排除这种可能了。

如果能排除字体文件的原因,那么程序没有问题
我回复中的截图就是你的程序产生的


字体 elephant 的文件名为 elephnt.ttf 斜体的是 elephnti.ttf 请认真核实
判断是否是字体文件造成的,可以用浏览器直接运行验证码程序


运行http://127.0.0.1/config/code.php验证码文件时,页面中啥也不显示,空白一遍。也许你看到这里,嘴里会脱口而出,那肯定验证码有问题,没错!要是没问题,我也不发帖问了。

单独运行的时候,ROOT_PATH 是没有定义的!你得定义一下

单独运行的时候,ROOT_PATH 是没有定义的!你得定义一下


定义了,因为引入定义ROOT_PATH字的系统常量文件了。

但是在你贴出的程序里是看不到这一点的

另外你可打开 php 的错误显示功能,看看是否有错误信息出现
单独运行验证码程序时,如果出现错误且错误显示功能未打开,则会出现空白页(这实际是500错的表现)
如果出现一个叉,则表示程序有非致命错误,此时应注释掉 header 函数。根据夹杂在乱码中的错误信息排错

但是在你贴出的程序里是看不到这一点的

另外你可打开 php 的错误显示功能,看看是否有错误信息出现
单独运行验证码程序时,如果出现错误且错误显示功能未打开,则会出现空白页(这实际是500错的表现)
如果出现一个叉,则表示程序有非致命错误,此时应注释掉 header 函数。根据夹杂在乱码中的错误信息排错




干脆我把源码发上,你自己看能显示吗,文件不大http://115.com/lb/5lbdn1thp19o

看得到,下不下了
你换个网盘看看

看得到,下不下了
你换个网盘看看


本来想传到百度云了,没想到百度云网盘出问题了,这一扫黄,百度云一直不好用了。把正常人使用的环境也破坏了
传到360网盘中了
http://yunpan.cn/cAkyJ6ddksXJJ (提取码:a62a)

很正常。

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();




如果出现空白,可以看看apache error log,看看有什么错误提示。

如果是elephant.ttf文件问题,可以下载一个新的elephant.ttf文件试试。
我测试的是用 http://www.font5.com.cn/font_download.php?id=8944&part=1279954173这个的,可以换这个试试。

很正常。

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();




如果出现空白,可以看看apache error log,看看有什么错误提示。

提示这个:
[Sun Nov 23 11:07:43 2014] [error] [client 127.0.0.1] PHP Fatal error: Call to a member function fetch_object() on a non-object in D:\\APMServ5.3.0\\www\\htdocs\\model\\Model.class.php on line 43

如果是elephant.ttf文件问题,可以下载一个新的elephant.ttf文件试试。
我测试的是用 http://www.font5.com.cn/font_download.php?id=8944&part=1279954173这个的,可以换这个试试。


用你这个也不能显示,路径是没有问题的,我试了,都正常,比如我把code.php换一个图片就能显示,说明路径是正确的.
字体文件也没 有坏,能打开。

http://yunpan.cn/cAkZubfk5Ikte (提取码:03d7)

我把我用的服务器集成环境发上来吧,里面也有源码,你们在你们的操作系统环境中能正常显示验证吗,也许是我的系统有问题?

提示这个:
[Sun Nov 23 11:07:43 2014] [error] [client 127.0.0.1] PHP Fatal error: Call to a member function fetch_object() on a non-object in D:\\APMServ5.3.0\\www\\htdocs\\model\\Model.class.php on line 43

单独运行验证码的页面,会有这个提示?
代码中根本就没有调用Model.class.php

好奇怪的情况

把你的 config/code.php 改成这样

doimg();$_SESSION['code'] = $_vc->getCode();?>

这样可以使其他任何输出都不会影响到图片
当然错误显示还是要打开的(php.ini 中 display_errors = On),这个开关很重要,他可以帮助你写出健壮的程序

你的这个系统有很多毛病,比如
model/NavModel.class.php 中的
		//拦截器(__set)		private function __set($_key, $_value) {			$this->$_key = Tool::mysqlString($_value);		}				//拦截器(__get)		private function __get($_key) {			return $this->$_key;		}
魔术方法是不能设为私有的,不然就无效了,并且会报错

config/profile.inc.php (用于系统初始化设定)中有
	define('PREV_URL',$_SERVER["HTTP_REFERER"]);

这就导致直接浏览器访问时会有使用未定义下标的警告

另外 include/Tool.class.php 至少有两处因参数不合法引起的非致命错误

这都是在直接浏览器访问 config/code.php 时能看到的错误
想必其他部分都会有类似错误

我把你的 CMS 项目改名为 CMS_1
并按 #25 修改了 code.php

我把你的 CMS 项目改名为 CMS_1
并按 #25 修改了 code.php



改成这样
doimg();$_SESSION['code'] = $_vc->getCode();?>
还是不行,打开code.php页面时还是空白;
错误日志中提示 [Sun Nov 23 15:31:45 2014] [error] [client 127.0.0.1] PHP Fatal error: Call to a member function fetch_object() on a non-object in D:\\APMServ5.3.0\\www\\htdocs\\model\\Model.class.php on line 43

看来还是Model.class.php这个类没有加载进来,那么怎么加载呢

提示这个:
[Sun Nov 23 11:07:43 2014] [error] [client 127.0.0.1] PHP Fatal error: Call to a member function fetch_object() on a non-object in D:\\APMServ5.3.0\\www\\htdocs\\model\\Model.class.php on line 43

单独运行验证码的页面,会有这个提示?
代码中根本就没有调用Model.class.php


这里如何解决呢

我在php.ini中 开启错误提示后,进入127.0.0.1/config/code.php页面有这一提示
Notice: Undefined index: HTTP_REFERER in D:\APMServ5.3.0\www\htdocs\config\profile.inc.php on line 26 Warning: The magic method __set() must have public visibility and cannot be static in D:\APMServ5.3.0\www\htdocs\model\NavModel.class.php on line 12 Warning: The magic method __get() must have public visibility and cannot be static in D:\APMServ5.3.0\www\htdocs\model\NavModel.class.php on line 17 Fatal error: Call to a member function fetch_object() on a non-object in D:\APMServ5.3.0\www\htdocs\model\Model.class.php on line 43
这里也不知如何解决呢

不会的,因为我用的就是你的程序
并且刚才又用你的包覆盖了一下

你可先一步一步来,把提示的错误信息全部改完

不会的,因为我用的就是你的程序
并且刚才又用你的包覆盖了一下

你可先一步一步来,把提示的错误信息全部改完


算了不改了,这是一个叫李炎恢的人做的一个php教程的原码,没想到错误百出,本来想装上学一下他的源码结构,没想到这样的教程也敢卖,不知误了多少人。这还没学呢,先给他纠错,呵呵。

好奇怪的情况



通过这事发一下感慨,中国人搞啥都是胡弄,本来这个源码是一个叫李炎恢的人出的php教程,叫什么php第二季,他是专门做教程的,并且卖的。本想装上学学,没想到的是,他本身就纯在很多问题,一个存在很多问题的人教别人,然后学习者还没有学呢,先给他纠错,等错误全纠正了,回头一看,原来自己是搞手,呵呵,
另外发一个感慨是,本来想通过百度云把源码发上去,供大家一块研究交流,没想到今天百度云抽风了,突然不让上传了,大家知道百度云一直被网上扫黄的,但是这种扫黄有意无意的把正常使用百度云的人也给封杀了,这叫宁可错杀一千,不放走一个,在中国搞点事真是不容易啊

看得到,下不下了
你换个网盘看看


结果我现在都不传了,现在检查百度云,又恢复正常了,真让人发指!看来百度云干脆解散算了。

学到东西了,谢谢楼主66666666666666666666666