一個高難度問題,關於驗證碼不顯示

來源:互聯網
上載者:User
一般驗證碼不顯示,無處乎是因為: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頁,但是這個頁我看了一下寫的很規範。

驗證碼

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();


我明白了,我沒有把這個源碼放在根目錄下造成,謝謝,功力深厚啊

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.