您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息

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

2026/2/4 22:27:13发布9次查看
一般验证码不显示有下面几种情况
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
该用户其它信息

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录 Product