验证码计算不起作用

时间:2014-05-07 19:41:32

标签: php html captcha

我是PHP的新手,我正在研究生成总和的验证码。
例如17 + 4

它的计算效果很好,但我希望输入字段中的人物类型与PHP计算然后回显的答案相匹配。如果这是错的,那么回复另一句话。请帮帮我!

<html>
    <head>
        <style>
            .wrapper {
                width: 100%;
                text-align: center;
                margin-top: 100px;
            }
        </style>
    </head>
    <body>
        <div class="wrapper">
            <?PHP
                $first_calculation = rand(5, 20);
                $second_calculation = rand(2, 10);
                $answer = $first_calculation + $second_calculation;
                $captcha = $_POST['captcha'];
                if (isset($captcha)) {
                    if ($captcha == $answer) {
                        echo '<font color="green">Correct!</font>';
                    }
                    else {
                        echo '<font color="red">The answer you filled in was wrong. Please try again.</font>';
                    }
                }
            ?>
            <form action="index.php" method="post">
                <h3>Prove that you're human!</h3>
                <?PHP
                    echo $first_calculation
                ?> +
                <?PHP
                    echo $second_calculation
                ?>
                <input type="text" name="captcha" value="">
                <input type="submit" value="submit">
            </form>
        </div>
    </body>
</html>

1 个答案:

答案 0 :(得分:0)

更安全的做法......

创建一个名为captcha.php的文件 并将以下代码放在那里:

<?php
function str_write_captcha($mime_type,$s_path,$string)
{
    if((strcmp("jpeg",$mime_type)==0) ||( strcmp("jpg",$mime_type)==0))
    {
        header("Content Type:image/jpeg");  // Set the mime type as jpeg
        $im     = imagecreatefromjpeg($s_path);  //load the image
        $orange = imagecolorallocate($im, 45, 45, 45);  // pass the color of the font to be written
        $px     = (imagesx($im) - 9 * strlen($string)) / 2;  //set the  X position of the string
        $py     = imagesy($im) - 36 ;             //set the  Y position of the string
        imagestring($im, 6, $px, $py, $string, $orange); //Write the string on the image
        imagejpeg($im);  //Generate the jpeg image
        return (imagejpeg($im));
    }
    else if(strcmp("png",$mime_type)==0)
    {
        header("Content Type:image/png");  // Set the mime type as jpeg
        $im     = imagecreatefrompng($s_path); //load the image
        $orange = imagecolorallocate($im, 45, 45, 45); // pass the color of the font to be written
        $px     = (imagesx($im) - 9 * strlen($string)) / 2; //set the  X position of the string
        $py     = imagesy($im) - 36 ;  //set the  Y position of the string
        imagestring($im, 5, $px, $py, $string, $orange);  //Write the string on the image
        imagepng($im);  //Generate the png image
        return (imagepng($im));
    }
    else
    {
        print("<B>UNKNOWN MIME TYPE !!! </B>");
    }
}

if(!$_SESSION['capt'] {
    $first_calculation = rand(5, 20);
    $second_calculation = rand(2, 10);
    $string = $first_calculation + $second_calculation;
    $_SESSION['capt'] = $answer;
}
echo str_write_captcha('png','/images/my_background_image.png',$string)
?>

在您要包含验证码的页面中,只需使用:

<img src="captcha.php">

坚持你的逻辑:

<html>
    <head>
        <style>
            .wrapper {
                width: 100%;
                text-align: center;
                margin-top: 100px;
            }
        </style>
    </head>
    <body>
        <div class="wrapper">
            <?PHP
                $captcha = $_POST['captcha'];
                if (isset($captcha)) {
                    if ($captcha == $_SESSION['capt']) {
                        echo '<font color="green">Correct!</font>';
                        // UNSET the session 
                        // $_SESSION['capt'] = '';
                    }
                    else {
                        echo '<font color="red">The answer you filled in was wrong. Please try again.</font>';
                    }
                }
            ?>
            <form action="index.php" method="post">
                <h3>Prove that you're human!</h3>
                <img src="captcha.php">
                <input type="text" name="captcha" value="">
                <input type="submit" value="submit">
            </form>
        </div>
    </body>
</html>