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

一个高难度问题,关于验证码不显示

Jun 23, 2016 pm 01:47 PM

一般验证码不显示,无处乎是因为:1 有 BOM 头,2  extension=php_gd2.dll没有开启(即去掉分号)

但我这个不是上面这两个原因,因为我都检查了。
论坛里还有解决办法说是在页面中加上开头加上 ini_set('display_errors', 'Off');我也加也,(见这个帖子:http://bbs.csdn.net/topics/350011289)还是不行,
但说也奇怪,其他的cms程序却显示验证码,我用的php版本是PHP Version 5.3.28
要不我说这是高难度问题呢,这到底是怎么回事呢
 


回复讨论(解决方案)

不是明摆的吗?你把那五个错误纠正了就可以了

您要打开的页面,不应该是生成验证码的页面么?否则,你开了错误显示,又如何呢?

和个没有关系,那个是警告信息,和验证码无关,另 就是用了它error_reporting(E_ALL & ~E_NOTICE);验证码还是不显示。

贴出你的代码!

我不知道你是打算结决问题,还是在逗闷子

我建议不管你是啥,只要不喜欢我的问题的人请你离开我的帖子,世界很大,你感兴趣的地方很多,你给我多少钱我逗你玩呢?你就是给我钱我还考虑陪你玩不!

我不指望非得我的问题非得有人回答,我不指望非得有人对我的问题感兴趣!逗乐子,你不照照镜子,你是谁啊!你有这个资格吗

贴出你的代码!

我不知道你是打算结决问题,还是在逗闷子


从你的诸多回帖的方式来看,能判断出,你也就是php里面三四流角色,你不能解决的奇怪问题多着呢,别以为你认为很简单的问题就似乎不算问题,你差远去了!真正的高手,你看哪个象你这种嘴脸,你充其量是对php似懂非懂的人,我也是,但我和你不一样的地方是,我很谦虚。

不同情况不同分析。
你的截图只是说明验证码图片显示不出来,而上面的notice和warning是login.php的警告和提示,与验证码是没有任何关系的。
可以贴出你生成验证码图片的代码吗,这样才能分析。

不同情况不同分析。
你的截图只是说明验证码图片显示不出来,而上面的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);		}			}
Copy after login

修改了一下,ok了,主要是字体那里,你没有定义ROOT_PATH,导致获取不到 $this->font = ROOT_PATH.'/font/elephant.ttf';
把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();
Copy after login


我明白了,我没有把这个源码放在根目录下造成,谢谢,功力深厚啊

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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
4 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)

Working with Flash Session Data in Laravel Working with Flash Session Data in Laravel Mar 12, 2025 pm 05:08 PM

Laravel simplifies handling temporary session data using its intuitive flash methods. This is perfect for displaying brief messages, alerts, or notifications within your application. Data persists only for the subsequent request by default: $request-

cURL in PHP: How to Use the PHP cURL Extension in REST APIs cURL in PHP: How to Use the PHP cURL Extension in REST APIs Mar 14, 2025 am 11:42 AM

The PHP Client URL (cURL) extension is a powerful tool for developers, enabling seamless interaction with remote servers and REST APIs. By leveraging libcurl, a well-respected multi-protocol file transfer library, PHP cURL facilitates efficient execution of various network protocols, including HTTP, HTTPS, and FTP. This extension offers granular control over HTTP requests, supports multiple concurrent operations, and provides built-in security features.

Simplified HTTP Response Mocking in Laravel Tests Simplified HTTP Response Mocking in Laravel Tests Mar 12, 2025 pm 05:09 PM

Laravel provides concise HTTP response simulation syntax, simplifying HTTP interaction testing. This approach significantly reduces code redundancy while making your test simulation more intuitive. The basic implementation provides a variety of response type shortcuts: use Illuminate\Support\Facades\Http; Http::fake([ 'google.com' => 'Hello World', 'github.com' => ['foo' => 'bar'], 'forge.laravel.com' =>

12 Best PHP Chat Scripts on CodeCanyon 12 Best PHP Chat Scripts on CodeCanyon Mar 13, 2025 pm 12:08 PM

Do you want to provide real-time, instant solutions to your customers' most pressing problems? Live chat lets you have real-time conversations with customers and resolve their problems instantly. It allows you to provide faster service to your custom

Explain the concept of late static binding in PHP. Explain the concept of late static binding in PHP. Mar 21, 2025 pm 01:33 PM

Article discusses late static binding (LSB) in PHP, introduced in PHP 5.3, allowing runtime resolution of static method calls for more flexible inheritance.Main issue: LSB vs. traditional polymorphism; LSB's practical applications and potential perfo

Framework Security Features: Protecting against vulnerabilities. Framework Security Features: Protecting against vulnerabilities. Mar 28, 2025 pm 05:11 PM

Article discusses essential security features in frameworks to protect against vulnerabilities, including input validation, authentication, and regular updates.

Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Apr 05, 2025 am 12:04 AM

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

See all articles