一个高难度问题,关于验证码不显示
一般验证码不显示,无处乎是因为: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); } }
修改了一下,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();
我明白了,我没有把这个源码放在根目录下造成,谢谢,功力深厚啊

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

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

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











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 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 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.

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.

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 is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

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.

Using preprocessing statements and PDO in PHP can effectively prevent SQL injection attacks. 1) Use PDO to connect to the database and set the error mode. 2) Create preprocessing statements through the prepare method and pass data using placeholders and execute methods. 3) Process query results and ensure the security and performance of the code.
