验证码验证无效

时间:2012-03-27 18:27:27

标签: php captcha

我有以下代码验证 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开始得到一些尴尬的代码。我的错在哪里?有人可以帮帮我吗?

1 个答案:

答案 0 :(得分:1)

您需要声明有关内容类型的标题,以便浏览器将其识别为图像。试试这个

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