2012-03-27 139 views
0

我有以下代碼驗證 PHP腳本:不工作圖形驗證碼驗證

if(empty($_POST['captcha_code'])) { 
    $error = 1; 
    $code[3] = 'color:#FF0000;'; 
    } else { 
    include_once "formfiles/captcha.php"; 
     $randomnr = new Securimage(); 
    $valid = $randomnr->check($_POST['captcha_code']); 

    if(!$valid) { 
     $error = 1; 
     $code[3] = 'color:#FF0000;'; 
     $code[4] = '<strong><span style="color:#FF0000;">Incorrect code</span></strong>'; 
    } 
    } 

驗證碼 PHP代碼:

<?php 
    Securimage(); 
    exit(); 

    function Securimage() 
    { 
    $randomnr = rand(1000, 9999); 
    $_SESSION['randomnr2'] = md5($randomnr); 

    $im = imagecreatetruecolor(100, 38); 

    $white = imagecolorallocate($im, 255, 255, 255); 
    $grey = imagecolorallocate($im, 150, 150, 150); 
    $black = imagecolorallocate($im, 0, 0, 0); 

    imagefilledrectangle($im, 0, 0, 200, 35, $black); 

    //path to font - this is just an example you can use any font you like: 

    $font = dirName(__FILE__).'/calvin.ttf'; 

    imagettftext($im, 20, 4, 22, 30, $grey, $font, $randomnr); 

    imagettftext($im, 20, 4, 15, 32, $white, $font, $randomnr); 

    imagegif($im); 
    imagedestroy($im); 
    } 
?> 

提交我的形式後,我總是得到一個尷尬以gif開頭的一段代碼。我的錯在哪裏?有人能幫幫我嗎?

+0

檢查方法是如何工作的? – Daxcode 2012-03-27 18:31:10

+0

'imagegif($ im)'將輸出原始的.gif圖像字節到瀏覽器。如果沒有適當的內容類型頭文件(例如'image/gif'),瀏覽器就會將其視爲純文本並嘗試顯示它。如果該腳本直接鏈接,而不是通過''鏈接,那麼您將永遠不會獲得任何東西,只會產生二進制垃圾。 – 2012-03-27 18:49:49

回答

1

您需要將有關內容類型的decalare標題識別爲圖像。試試這個

header('Content-Type: image/gif'); 
Securimage(); 
exit(); 
+0

我總是收到錯誤信息,無法修改標題信息。我該如何解決這個問題? – 2012-03-27 19:45:28

+0

確保你在'<?php'或'?>'標籤之前沒有空格。 – safarov 2012-03-27 19:49:29