Home > Backend Development > PHP Tutorial > 一个高难度有关问题,关于验证码不显示

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

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
Release: 2016-06-13 12:07:39
Original
938 people have browsed it

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

本帖最后由 ziluoapo4 于 2014-10-12 17:33:16 编辑 一般验证码不显示,无处乎是因为: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
要不我说这是高难度问题呢,这到底是怎么回事呢
 
------解决思路----------------------
修改了一下,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__));

完整测试代码如下:
<br>//验证码类<br>define('ROOT_PATH', dirname(__FILE__));<br><br>//验证码类<br>class ValidateCode {<br>    private $charset = 'abcdefghkmnprstuvwxyzABCDEFGHKMNPRSTUVWXYZ23456789';    //随机因子<br>    private $code;                            //验证码<br>    private $codelen = 4;                    //验证码长度<br>    private $width = 130;                    //宽度<br>    private $height = 50;                    //高度<br>    private $img;                                //图形资源句柄<br>    private $font;                                //指定的字体<br>    private $fontsize = 20;                //指定字体大小<br>    private $fontcolor;                        //指定字体颜色<br>     <br>    //构造方法初始化<br>    public function __construct() {<br>        $this->font = ROOT_PATH.'/font/elephant.ttf';<br>    }<br>     <br>    //生成随机码<br>    private function createCode() {<br>        $_len = strlen($this->charset)-1;<br>        for ($i=0;$icodelen;$i++) {<br>            $this->code .= $this->charset[mt_rand(0,$_len)];<br>        }<br>    }<br>     <br>    //生成背景<br>    private function createBg() {<br>        $this->img = imagecreatetruecolor($this->width, $this->height);<br>        $color = imagecolorallocate($this->img, mt_rand(157,255), mt_rand(157,255), mt_rand(157,255));<br>        imagefilledrectangle($this->img,0,$this->height,$this->width,0,$color);<br>    }<br>     <br>    //生成文字<br>    private function createFont() {   <br>        $_x = $this->width / $this->codelen;<br>        for ($i=0;$icodelen;$i++) {<br>            $this->fontcolor = imagecolorallocate($this->img,mt_rand(0,156),mt_rand(0,156),mt_rand(0,156));<br>            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]);<br>        }<br>    }<br>     <br>    //生成线条、雪花<br>    private function createLine() {<br>        for ($i=0;$i            $color = imagecolorallocate($this->img,mt_rand(0,156),mt_rand(0,156),mt_rand(0,156));<br>            imageline($this->img,mt_rand(0,$this->width),mt_rand(0,$this->height),mt_rand(0,$this->width),mt_rand(0,$this->height),$color);<br>        }<br>        for ($i=0;$i
                 
              
              
        
            
Copy after login
Related labels:
source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template