简体   繁体   中英

Captcha code validation not working

I have the following code validation php script:

  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>';
    }
  }

and this captcha php code:

<?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);
    }
?>

After submitting my form I always get an awkward bit of code starting with gif. Where is my fault? Can somebody please help me?

You need to decalare header about the content type to browser recognize it as image. Try this

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM