在下面的代码中,没有显示验证码图片。 我在代码中没有任何错误,所以我无法理解什么行不正确

generete.php

this page have to make a captcha picture to user but it did not work for me

<?php



    session_start();
        header('Content-Type: image/jpeg');

        $text=$_SESSION['secure'];
        $font_size=30;

    $image_width=200;
    $image_height=40;

    /*use width and height to make image 
    $image=imagecreate($image_width,$image_height);
    imagecolorallocate($image,255,255,255);
    $text_color=imagecolorallocate($image,0,0,0);
    /* add a few lines to hard to reading*/
    for($x=1;$x<=20;$x++){
      $x1=rand(1,100);
      $x2=rand(1,100);
      $y1=rand(1,100);
      $y2=rand(1,100);

      imageline($image,$x1,$x2,$y1,$y2,$text_color);
    }

    imagettftext($image,$font_size,0,15,30,$text_color,'B Elm.ttf',$text);

    imagejpeg($image);

?>

index.php this page check if user enter correct value that shows in picture or not, if is correct show the 'A match.'

     <?php
    session_start();

    if(!isset($_POST['secure'])){
      $_SESSION['secure']=rand(1000,9999);
    }else {
      if($_SESSION['secure']==$_POST['secure']){
        echo'A match.';
      } else{

/*if user enter wrong characters, can refresh and get new code.*/


        echo'incorrect , not match.';
        $_SESSION['secure']=rand(1000,9999);
      }
    }
    ?>

    <img src="generate.php" /> <br>

    <form action="" method="POST">
      type the value you see: <input name="secure" type="text" size="4" maxlength="4">
      <input name="Submit" type="submit" value="Submit">
    </form>
 <?php
$randnum=rand(1000,9999);
if(isset($_POST['Submit'])){
    $randnumb = $_POST['randnum'];
    $secure = $_POST['secure'];
    if($randnumb==$secure){
        echo'A match.<br/><a href="index.php">refresh</a>';
    }else{
        echo'incorrect , not match.<br/><a href="index.php">refresh</a>';
    }
}else{
    echo $randnum;
}

?>

<form method="POST">
  type the value you see: 
  <input name="randnum" type="hidden" value="<?php echo $randnum; ?>" size="4" maxlength="4">
  <input name="secure" type="text" size="4" maxlength="4">
  <input name="Submit" type="submit" value="Submit">
</form>

This code show errors if you will open file generate.php it into separated browser tab and remove header function. I fixed this code and it show me image:

I make this changes:

  1. I add isset check for session
  2. You have non closed comment in this line: use width and height to make image
  3. Also you must specify path to font as ./B Elm.ttf
  4. Don't forget store font file B Elm.ttf near file generate.php

See my code below:

<?php
session_start();
    header('Content-Type: image/jpeg');

    $text=isset($_SESSION['secure']) ? $_SESSION['secure'] : 'secure';
    $font_size=30;

$image_width=200;
$image_height=40;

/*use width and height to make image*/
$image=imagecreate($image_width,$image_height);
imagecolorallocate($image,255,255,255);
$text_color=imagecolorallocate($image,0,0,0);
/* add a few lines to hard to reading*/
for($x=1;$x<=20;$x++){
  $x1=rand(1,100);
  $x2=rand(1,100);
  $y1=rand(1,100);
  $y2=rand(1,100);

  imageline($image,$x1,$x2,$y1,$y2,$text_color);
}

imagettftext($image,$font_size,0,15,30,$text_color,'./B Elm.ttf',$text);

imagejpeg($image);

?>