数学验证码给出错误的价值

时间:2014-08-09 17:37:41

标签: php html

以下匹配的captcah表单不正确。在正确值和输入的正确值的情况下,输出将以 “验证码不正确!” 的形式出现。我试过但无法找到解决方案。产品代码如下:

form.php的

<?php
session_start();
echo '<form action="check.php" method="post">';
$rand_int1 = substr(mt_rand(),0,1);
$rand_int2 = substr(mt_rand(),0,1);
$cap_ans = $rand_int1 + $rand_int2;
$_SESSION['cap_ans'] = $cap_ans;
echo 'What is '.$rand_int1.' + '.$rand_int2.' ?<br>
<input type="text" name="cap_inp">
<input type="submit" value="SUBMIT">
</form>';
?>

CHECK.PHP

<?php
session_start();
$cap_inp = $_POST['cap_inp'];
$cap_ans = $_SESSION['cap_ans'];

if($cap_inp != $cap_ans) {
    echo 'Captcha is incorrect!';
}
else {
    echo 'Captcha is correct, congratulations! :)';
}
?>

任何人都可以帮我/建议正确的代码吗?

1 个答案:

答案 0 :(得分:0)

我尝试了你的代码并且运行良好。问题必须在其他地方,在你没有发布的代码中。

if(!empty($_POST['cap_inp'])){
    $cap_inp = $_POST['cap_inp'];
    $cap_ans = $_SESSION['cap_ans'];

    if($cap_inp != $cap_ans) {
            echo 'Captcha is incorrect!';
    }
    else {
            echo 'Captcha is correct, congratulations! :)';
    }
} else {
    $rand_int1 = substr(mt_rand(), 0, 1);
    $rand_int2 = substr(mt_rand(), 0, 1);

    $cap_ans = $rand_int1 + $rand_int2;

    $_SESSION['cap_ans'] = $cap_ans;
    echo '
        <form method="post">
            What is '.$rand_int1.' + '.$rand_int2.'?<br>
            <input type="text" name="cap_inp">
            <input type="submit" value="SUBMIT">
        </form>
    ';
}

返回的

  

Captcha是正确的,恭喜! :)