2012-03-27 123 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