提示这个: [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
提示这个: [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
改成这样<?phprequire substr(dirname(__FILE__),0,-7).'/init.inc.php'; ob_clean(); //加上这句 $_vc = new ValidateCode(); $_vc->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这个类没有加载进来,那么怎么加载呢
我在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 这里也不知如何解决呢
define('ROOT_PATH',dirname(__FILE__));
public function __construct() {
$this->font = ROOT_PATH.'/font/elephant.ttf';
}
或者说这个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);
}
}
?>
第一,这个字体文件有问题,即兼容性上的问题,因为我的系统是win8 64位的,我确定是否字体对于操作系统也有兼容性这一说;
第二,路径问题,但是即上面所罗列的设置,应该没有问题吧,常规的写法都是这么写啊。
至于啥GD库开启啥的,这些,都排除了,还有啥BOM头的,也排除了,那是啥原因呢
字体 elephant 的文件名为 elephnt.ttf 斜体的是 elephnti.ttf 请认真核实
判断是否是字体文件造成的,可以用浏览器直接运行验证码程序
单独运行验证码程序时,如果出现错误且错误显示功能未打开,则会出现空白页(这实际是500错的表现)
如果出现一个叉,则表示程序有非致命错误,此时应注释掉 header 函数。根据夹杂在乱码中的错误信息排错
传到360网盘中了
http://yunpan.cn/cAkyJ6ddksXJJ (提取码:a62a)
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();
如果出现空白,可以看看apache error log,看看有什么错误提示。
我测试的是用http://www.font5.com.cn/font_download.php?id=8944&part=1279954173这个的,可以换这个试试。
提示这个:
[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
字体文件也没 有坏,能打开。
[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
require substr(dirname(__FILE__),0,-7).'/init.inc.php';
ob_clean(); //加上这句
$_vc = new ValidateCode();
$_vc->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 时能看到的错误
想必其他部分都会有类似错误
并按 #25 修改了 code.php
改成这样<?phprequire substr(dirname(__FILE__),0,-7).'/init.inc.php';
ob_clean(); //加上这句
$_vc = new ValidateCode();
$_vc->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这个类没有加载进来,那么怎么加载呢
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教程,叫什么php第二季,他是专门做教程的,并且卖的。本想装上学学,没想到的是,他本身就纯在很多问题,一个存在很多问题的人教别人,然后学习者还没有学呢,先给他纠错,等错误全纠正了,回头一看,原来自己是搞手,呵呵,
另外发一个感慨是,本来想通过百度云把源码发上去,供大家一块研究交流,没想到今天百度云抽风了,突然不让上传了,大家知道百度云一直被网上扫黄的,但是这种扫黄有意无意的把正常使用百度云的人也给封杀了,这叫宁可错杀一千,不放走一个,在中国搞点事真是不容易啊首先,课程学习和开源系统完全不同,开源系统会说明PHP5.2还是PHP5.3或PHP5.4,甚至数据库版本也有一定影响。你用开源系统的理念,去安装学习课程的源代码,本身就是想法天真,因为课程没有对所有环境做测试。如果你从第一课开始学习,学完,自己动手设计,就不会有这些问题了。刚才版主说private有问题,那是因为当时的版本支持private,升级到PHP5.3就必须public了。当然,如果移植到php5.3或php5.4,的确要修改不少地方,对于4年前的课程,用现在的PHP版本去运行,肯定报错!而我们最新课程,都是按照PHP5.4。当然,现在PHP5.5,5.6又推出了。所以,课程学习学习的是视频,而不是下载课程源码学习,因为你根本不知道老师在课程怎么设置环境的。
我举个自己亲身经历的例子.一个登录问题,我登录一个网站,我的电脑用尽N天N种方法,并远程让这个站的站长亲自帮我登录或找问题所在,可是怎么登录也上不去,重做系统也不行,,换电脑也不行(我家两台电脑),我家adsl的,ip也换了N天N遍!.....可是上邻居家登录就上去了,他家也是adsl的,我后来把我自己的电脑搬到他家也登录上了,,,让我证明了不是电脑原因!
过了几个月,我突然又想到了这个网站,在自己家也登录上去了
呵呵,是不是见鬼了 嗯,但实事就是实事! 这是我自己亲身经历的!我想说的是,, 有些事物,解决不了的, 想想其它的办法,,因为他本身没错,就像我的这个事情,,不是站长故意封我ip或什么原因,也不是电脑原因等等。
也许你的这个程序放在其它人手上就正常,,但自己弄却又不正常。为什么呢。
不要再纠结为什么了,,,,有些东西很邪门,,谁也说不清!