Table of Contents
回复讨论(解决方案)
Home Backend Development PHP Tutorial 一个超难的问题,关于验证码不显示

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

Jun 23, 2016 pm 01:44 PM

一般验证码不显示有下面几种情况
1、GD库没有开启,2、用记事本打开文件了,结果有BOM头
3、 的前面有空格了
但是我的验证码不显示和上述三种都没有关系,因为我检查了

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

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


回复讨论(解决方案)

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

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


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

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

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

<?php	//验证码类	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;$i<$this->codelen;$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;$i<$this->codelen;$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;$i<6;$i++) {				$color = imagecolorallocate($this->img,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;$i<100;$i++) {				$color = imagecolorallocate($this->img,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);		}			}	?>
Copy after login

这一问题有几种可能
第一,这个字体文件有问题,即兼容性上的问题,因为我的系统是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;$i<$this->codelen;$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;$i<$this->codelen;$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;$i<6;$i++) {                $color = imagecolorallocate($this->img,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;$i<100;$i++) {                $color = imagecolorallocate($this->img,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();
Copy after login
Copy after login




如果出现空白,可以看看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;$i<$this->codelen;$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;$i<$this->codelen;$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;$i<6;$i++) {                $color = imagecolorallocate($this->img,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;$i<100;$i++) {                $color = imagecolorallocate($this->img,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();
Copy after login
Copy after login




如果出现空白,可以看看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 改成这样

<?phprequire substr(dirname(__FILE__),0,-7).'/init.inc.php';ob_clean(); //加上这句$_vc = new ValidateCode();$_vc->doimg();$_SESSION['code'] = $_vc->getCode();?>
Copy after login
Copy after login

这样可以使其他任何输出都不会影响到图片
当然错误显示还是要打开的(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;		}
Copy after login
魔术方法是不能设为私有的,不然就无效了,并且会报错

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

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

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

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

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

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



改成这样
<?phprequire substr(dirname(__FILE__),0,-7).'/init.inc.php';ob_clean(); //加上这句$_vc = new ValidateCode();$_vc->doimg();$_SESSION['code'] = $_vc->getCode();?>
Copy after login
Copy after login
还是不行,打开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

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Mandragora: Whispers Of The Witch Tree - How To Unlock The Grappling Hook
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1666
14
PHP Tutorial
1273
29
C# Tutorial
1253
24
Explain secure password hashing in PHP (e.g., password_hash, password_verify). Why not use MD5 or SHA1? Explain secure password hashing in PHP (e.g., password_hash, password_verify). Why not use MD5 or SHA1? Apr 17, 2025 am 12:06 AM

In PHP, password_hash and password_verify functions should be used to implement secure password hashing, and MD5 or SHA1 should not be used. 1) password_hash generates a hash containing salt values ​​to enhance security. 2) Password_verify verify password and ensure security by comparing hash values. 3) MD5 and SHA1 are vulnerable and lack salt values, and are not suitable for modern password security.

PHP and Python: Comparing Two Popular Programming Languages PHP and Python: Comparing Two Popular Programming Languages Apr 14, 2025 am 12:13 AM

PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

PHP: A Key Language for Web Development PHP: A Key Language for Web Development Apr 13, 2025 am 12:08 AM

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

PHP in Action: Real-World Examples and Applications PHP in Action: Real-World Examples and Applications Apr 14, 2025 am 12:19 AM

PHP is widely used in e-commerce, content management systems and API development. 1) E-commerce: used for shopping cart function and payment processing. 2) Content management system: used for dynamic content generation and user management. 3) API development: used for RESTful API development and API security. Through performance optimization and best practices, the efficiency and maintainability of PHP applications are improved.

How does PHP type hinting work, including scalar types, return types, union types, and nullable types? How does PHP type hinting work, including scalar types, return types, union types, and nullable types? Apr 17, 2025 am 12:25 AM

PHP type prompts to improve code quality and readability. 1) Scalar type tips: Since PHP7.0, basic data types are allowed to be specified in function parameters, such as int, float, etc. 2) Return type prompt: Ensure the consistency of the function return value type. 3) Union type prompt: Since PHP8.0, multiple types are allowed to be specified in function parameters or return values. 4) Nullable type prompt: Allows to include null values ​​and handle functions that may return null values.

The Enduring Relevance of PHP: Is It Still Alive? The Enduring Relevance of PHP: Is It Still Alive? Apr 14, 2025 am 12:12 AM

PHP is still dynamic and still occupies an important position in the field of modern programming. 1) PHP's simplicity and powerful community support make it widely used in web development; 2) Its flexibility and stability make it outstanding in handling web forms, database operations and file processing; 3) PHP is constantly evolving and optimizing, suitable for beginners and experienced developers.

PHP and Python: Code Examples and Comparison PHP and Python: Code Examples and Comparison Apr 15, 2025 am 12:07 AM

PHP and Python have their own advantages and disadvantages, and the choice depends on project needs and personal preferences. 1.PHP is suitable for rapid development and maintenance of large-scale web applications. 2. Python dominates the field of data science and machine learning.

PHP vs. Other Languages: A Comparison PHP vs. Other Languages: A Comparison Apr 13, 2025 am 12:19 AM

PHP is suitable for web development, especially in rapid development and processing dynamic content, but is not good at data science and enterprise-level applications. Compared with Python, PHP has more advantages in web development, but is not as good as Python in the field of data science; compared with Java, PHP performs worse in enterprise-level applications, but is more flexible in web development; compared with JavaScript, PHP is more concise in back-end development, but is not as good as JavaScript in front-end development.

See all articles