我想使用AJAX函数验证CAPTCHA



我的主页面是index.php。我使用CAPTCHA是因为,在该页面上提交表单后,它会检查CAPTCHA验证,如果值正确,则会转到form.php。如果值不正确,则它将保持在同一页上。

我的问题是,如果CAPTCHA值不正确,它会出现在index.php中,并且我在表单中输入的所有值都是null。我想要我在表单中输入的值,即使CAPTCHA是错误的,并要求新的CAPTCHA文本。

if(empty($_SESSION['6_letters_code'] ) || strcasecmp($_SESSION['6_letters_code'], $_POST['6_letters_code']) != 0)
{
    $errors .= "n The above code does not match!";
}
if(empty($errors))
{
    header('Location:form.php');
}
function IsInjected($str)
{
    $injections = array('(n+)',
        '(r+)',
        '(t+)',
        '(%0A+)',
        '(%0D+)',
        '(%08+)',
        '(%09+)'
    );
    $inject = join('|', $injections);
    $inject = "/$inject/i";
    if(preg_match($inject,$str))
    {
        return true;
    }
    else
    {
        return false;
    }
}
if(!empty($errors))
{
    header('Location:index.php');
    echo "The above code does not match!";  
}  

最简单的方法是将验证脚本放在表单的同一页上,验证后,如果正确,则将用户浏览器重定向到另一页,els让它们位于同一表单页上。。。

现在,假设您正在使用POST方法进行发送,请输入这样的输入字段。

<input type="text" name="username" value="<?php echo isset($_POST['username'])? $_POST['username']:'';?>"

其余部分相同。。

或者,如果您想在其他页面上使用验证逻辑,则使用SESSION变量在页面之间传递值。。。我希望我能回答你的要求。。其他评论..:)

最新更新