Captcha脚本生成一个失败响应


Captcha script generates a failure response

我刚开始学习php,试图用它来创建一个验证脚本的联系表单,但它总是返回一个错误,即使用户输入一个正确的数字:

 <?php
    session_start ();
    $_SESSION['safe']=rand(1000,9999);
    if(isset($_POST['nazwa']) && isset ($_POST['dane'])  &&isset($_POST['message'])) 
    {
        $name = $_POST['nazwa'];
        $email = $_POST['dane'];
        $message = $_POST['message'];
        if (!empty($name) && !empty($email) && !empty($message)) {
        if(strlen ($name)>50||strlen($email)>50||strlen($message)>1000) {echo 'Character limit exceeded ';  } else 
            {
                $from = 'z formularza nazwastrony';
                $to = 'roman.pliszka@yahoo.com';
                $subject = 'wiadomosc ze strony';
                $body = "From: $name'n E-Mail: $email'n Message:'n $message";
                if (!isset($_POST['check'])) {
                $_SESSION['safe']=rand(1000,9999);
                } else {
                    if ($_SESSION['safe'] == $_POST['check']) {
                        if ($_POST['submit']) 
                        { 
                        if (@mail ($to, $subject, $body, $from)) 
                    {
                        echo '<p>Message sent</p>';
                    } else 
                    {
                        echo '<p>An error occurred</p>';
                    } 
                }
    }else {
        echo 'Incorrect number';
        $_SESSION['safe']=rand(1000,9999);
    }
    }
    }
        } else {
            echo 'Please fill all of the fields';
        }
    } 
    ?> 

脚本使用generate.php生成图像:

<?php
session_start();
header('Content-type: image/jpeg');
$text = $_SESSION['safe'];
$font_size = 30;
$image_width = 100;
$image_height = 40;
$image = imagecreate ($image_width, $image_height);
imagecolorallocate($image, 255, 255, 255);
$text_color = imagecolorallocate($image, 0, 0, 0);
for ($x=1; $x<50; $x++) {
    $x1 = rand(1, 100);
    $y1 = rand(1, 100);
    $x2 = rand(1, 100);
    $y2 = rand(1, 100);
    imageline($image, $x1, $y1, $x2, $y2, $text_color);
}
imagettftext($image, $font_size, 0, 15, 30, $text_color, 'font.ttf', $text);
imagejpeg($image);
?>

基本上,在所有情况下,我得到"不正确的数字"。我的猜测是php会话更改生成另一个随机数,但我无法正确编写此代码。脚本和html表单中的所有名称都是相同的。如有任何帮助,我将不胜感激。

edit:这是一个带有用户输入的表单片段:

        <image src="generate.php" />
        <input type="text" name="check">    

明显的问题是语句的顺序。您应该在会话中生成新的随机代码之前处理POST数据。否则,您总是创建一个新代码,而不是用户发送的代码。我想稍微改变一下:

<?php
session_start();
// PROCESS POST DATA - RETURN CORRESPONDING MESSAGE
function processPostData() {
    $code="";
    // CHECK IF POST DATA WAS SENT
    if (isset($_POST["check"])) {
        $code=$_POST["check"];
    } else {
        return false;
    }
    // CHECK CODE
    if ($code != $_SESSION["safe"]) {
        return "<p>Incorrect number</p>";
    }
    // CHECK NON-EMPTY
    if (empty($_POST['nazwa']) || empty($_POST['dane']) || empty($_POST['message'])) {
        return "<p>Please fill all of the fields</p>";
    } else{
        // GET POST DATA
        $name = $_POST['nazwa'];
        $email = $_POST['dane'];
        $message = $_POST['message'];
    }   
    // CHECK MAX LENGTH
    if(strlen ($name)>50||strlen($email)>50||strlen($message)>1000) {
        return "<p>Character limit exceeded</p>";
    }
    /* VALIDATE DATA HERE: email (look for good validation scripts) */
    // SEND EMAIL
    if (@mail ($to, $subject, $body, $from)) {
        return "<p>Message sent</p>";
    } else {
        return "<p>An error occurred</p>";
    }
}
// PROCESS POST DATA - IF SET
echo processPostData();
// GENERATE NEW CODE EVERY TIME -! AFTER PROCESSING POST DATA
$_SESSION["safe"]=rand(1000,9999);
?>

p。问题是在你的代码的第3行-移动到最后,它应该工作。不过,我认为使用合适的API会更安全。我使用:Google的reCaptcha

我看不到形式的代码,但似乎,你使用错误的变量来生成验证码上的文本。尝试更换

$text = $_SESSION['bezpieczny'];

$text = $_SESSION['safe'];