一个高难度问题,关于验证码不显示 本帖最后由 ziluoapo4 于 2014-10-12 17:33:16 编辑 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 和个没有关系,那个是警告信息,和验证码无关,另 就是用了它error_reporting(E_ALL & ~E_NOTICE);验证码还是不显示。 不同情况不同分析。你的截图只是说明验证码图片显示不出来,而上面的notice和warning是login.php的警告和提示,与验证码是没有任何关系的。可以贴出你生成验证码图片的代码吗,这样才能分析。 说的没错,notice和warning是php使用中的一种警告,不影响代码运行,这个连刚学php的人都知道的常识。还有,我的这个问题其实就是验证类中验证码部分的问题,起初我还以为是BOM头的问题,但是保存为无BOM头的也不行,另外,这段代码是一个教程中的范例,在主讲人的环境下是可以显示的,当然他用的php版本低。我的高些,但是高的兼容低的啊。另外,GD也开启了,我也百度了不少方法没有奏效。所以才发此帖,关于代码很多,贴出来,估计大家也没有心情看。还有,就是帖也要帖验证码那个php页,但是这个页我看了一下写的很规范。 验证码<?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); } } 修改了一下,ok了,主要是字体那里,你没有定义ROOT_PATH,导致获取不到 $this->font = ROOT_PATH.'/font/elephant.ttf';把ROOT_PATH用define方式改为正确路径就可以的,我现在测试目录是这样的。test.phpfont/elephant.ttftest.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;$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(); 明摆着你妈逼啊,你连php入门级别都不够,竟然做版主,操,你怎么混进CSDN队伍中的啊 你瞧你个本事,你妈逼,你封我的帐户,我还可以注册啊,你妈逼,你有本事,让csdn限制注册,操,大不了,老子不来了,你妈逼论坛多了去了,高兴了,老子也建一个论坛,你个傻逼,瞧你那点出息! 你妈逼,一个版主竟然说别人发一个技术求答帖是在逗他玩,你妈逼,你这种态度要是被CSDN知道了,你妈逼,你还能干下去吗,你个缺心眼的货。 你妈逼我好久没在论坛里骂人了,N年前在中国ps联盟骂过一个象你样的傻逼版主,N年后你妈逼你又复活了,缺心眼的二货,我就纳闷为啥想你这种狗人总是能做版主呢,有何德何能啊,SB一个 php 文本框插件 求助PHP读取文件数据的MIME-TYPE的问题 关于FCKeditor的问题 问个关于php操作mysql数据库用户和权限的问题 如何批量修改mysql数据库中某个字段的值? iconv('', 'UTF-8', $str)问题,第一个参数没有表示什么意思呢 正则表达式。高手入!!! sql问题深入 错在哪里,在线等待!!!!!!!!!! mail()的问题! PHP session无效的问题 php 函数引用调用问题 PHP+mysql登陆问题和sesiion问题
你的截图只是说明验证码图片显示不出来,而上面的notice和warning是login.php的警告和提示,与验证码是没有任何关系的。
可以贴出你生成验证码图片的代码吗,这样才能分析。
还有,我的这个问题其实就是验证类中验证码部分的问题,起初我还以为是BOM头的问题,但是保存为无BOM头的也不行,另外,这段代码是一个教程中的范例,在主讲人的环境下是可以显示的,当然他用的php版本低。我的高些,但是高的兼容低的啊。
另外,GD也开启了,我也百度了不少方法没有奏效。所以才发此帖,关于代码很多,贴出来,估计大家也没有心情看。
还有,就是帖也要帖验证码那个php页,但是这个页我看了一下写的很规范。
<?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);
}
}
把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;$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();