该表单是一个简单的captcha,但它不会验证if语句中的第一个条件


the form is a simple captcha but it does not validate the first condition in my if statement

这是第一个保存为image.php的php脚本。这是代码:

<?php
$img = imagecreatetruecolor(200,80);
$white = imagecolorallocate($img, 255, 255, 255);
$black = imagecolorallocate($img, 0, 0, 0);
$red = imagecolorallocate($img, 255, 0, 0);
$grey = imagecolorallocate($img,150, 150, 150);
$pink = imagecolorallocate($img, 200, 0, 150);
function randomString($length){
    $chars = "abcdefghijkmnopqrstuvwxyz023456789";
    srand((double)microtime()*1000000);
    $str = "";
    $i = 0;
        while($i <= $length){
            $num = rand() % 33;
            $tmp = substr($chars, $num, 1);
            $str = $str . $tmp;
            $i++;
        }
    return $str;
}
for($i=1;$i<=rand(1,5);$i++){
    $color = (rand(1,2) == 1) ? $pink : $red;
    imageline($img,rand(5,70),rand(5,20), rand(5,70)+5,rand(5,20)+5, $color);
}
imagefill($img, 0, 0, $white);
$string = randomString(rand(2,5));
$_SESSION['string'] = $string;
imagettftext($img, 11, 0, 10, 20, $black, "calibri.ttf", $string);
header("Content-type: image/png");
imagepng($img);
imagedestroy($img);
?>

在上面的代码中,一切都正常。。。。现在我创建了一个新的php脚本,并将其命名为captcha.php并将此代码插入脚本中:

<?php
ob_start();
session_start();
if(!isset($_POST['submit'])){
    echo "<form method='"post'" action='"captcha.php'">'n";
    echo "<table border='"0'" cellspacing='"3'" cellpadding='"3'">'n";
    echo "<tr><td>Type The Letters You See Below Into the Box</td></tr>'n";
    echo "<tr><td align='"right'"><img src='"image.php'"></td></tr>'n";
    echo "<tr><td align='"right'"><input type='"text'" name='"image'"></td></tr>'n";
    echo "<tr><td align='"right'"><input type='"submit'" name='"submit'" value='"Check CAPTCHA'"></td></tr>'n";
    echo "</table></form>'n";
}else {
    $image = $_POST['image'];
    if ($image == $_SESSION['string'])
    {
        echo "<b>Great success!</b>'n";
    }
    else 
    {
        echo "<em>Failure!</em>'n";
    }
}
ob_end_flush();
?> 

我陷入困境的过去是:if ($image == $_SESSION['string']),它没有正确验证。

您还必须在image.php中启动会话。