Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/287.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用单页验证将ReCaptcha与PHP注释表单结合使用_Php_Forms_Recaptcha - Fatal编程技术网

如何使用单页验证将ReCaptcha与PHP注释表单结合使用

如何使用单页验证将ReCaptcha与PHP注释表单结合使用,php,forms,recaptcha,Php,Forms,Recaptcha,我已经成功地在我的电子邮件表单中使用了ReCaptcha,但我的评论部分不断收到垃圾邮件,所以我想在那里使用它 但是,我不确定如何实现reCaptcha的验证代码 我的电子邮件表单使用第二个页面发布数据,因此效果很好,但我的评论表单具有在同一页面上发布表单的所有php代码 当我试图像以前那样输入reCaptcha的验证码时,由于张贴码在同一页上,加载时整个页面变为空白 错误报告显示: 注意:第36行/\u assets/commentBox/podcastHeader.php中的未定义索引:re

我已经成功地在我的电子邮件表单中使用了ReCaptcha,但我的评论部分不断收到垃圾邮件,所以我想在那里使用它

但是,我不确定如何实现reCaptcha的验证代码

我的电子邮件表单使用第二个页面发布数据,因此效果很好,但我的评论表单具有在同一页面上发布表单的所有php代码

当我试图像以前那样输入reCaptcha的验证码时,由于张贴码在同一页上,加载时整个页面变为空白

错误报告显示:

注意:第36行/\u assets/commentBox/podcastHeader.php中的未定义索引:recaptcha\u challenge\u字段

注意:第37行/\u assets/commentBox/podcastHeader.php中的未定义索引:recaptcha\u response\u字段

未正确输入reCAPTCHA。返回并重试。(reCAPTCHA说:不正确的验证码sol)

所以我猜它试图马上找到reCaptcha表单,但失败了,因为还没有填写任何内容

这是我在表格中使用的相关代码:

<?php
require_once('../../_assets/recaptchalib.php');
$privatekey = "xxxxxx ";
$resp = recaptcha_check_answer ($privatekey,
                            $_SERVER["REMOTE_ADDR"],
                            $_POST["recaptcha_challenge_field"],
                            $_POST["recaptcha_response_field"]);

if (!$resp->is_valid) {
// What happens when the CAPTCHA was entered incorrectly
die ("The reCAPTCHA wasn't entered correctly. Go back and try it again." .
     "(reCAPTCHA said: " . $resp->error . ")");
} else {
// Your code here to handle a successful verification
if (isset($_POST['submit'])) {
$required_fields = array("author", "body");
validate_presences($required_fields);

if (empty($errors)) {
    $author = mysql_prep($_POST['author']);
    $body = mysql_prep($_POST['body']); 
    $page_name = ($_POST['page_name']);
    $current_time = date("Y-m-d H-i-s");
    
    $query  = "INSERT INTO comments (";
    $query .= "  author, body, page_name, created";
    $query .= ") VALUES (";
    $query .= "  '{$author}', '{$body}', '{$page_name}', '{$current_time}'";
    $query .= ")";
    $result = mysqli_query($connection, $query);
    
    if ($result) {
        redirect_to("{$url}{$anchor}");
    } else {
            // Failure
            $_SESSION["message"] = "There was an error that prevented the comment from being saved.";
    }
}
} else {
    $author = "";
    $body = "";
}
}
$display_comments = find_comments();
?>

$resp=repatcha\u check\u response($privatekey,
$\u服务器[“远程地址”],
$\u POST[“重演挑战场”],
$(POST[“recaptcha_response_field”])

这一行会立即检查结果,因为之前没有检查POST数据,所以页面在执行任何操作之前都会死掉

将支票放在条件内:

if (isset($_POST['submit'])) {
    $resp = recaptcha_check_answer(...);
    if (!$resp->is_valid) {
        // Wrong captcha
    }
}

我刚检查了我的reCAPTCHA代码…似乎和你的相同。等等…一个不同。我在我的isset中有我的php reCAPCTHA代码,包括die()。在你的isset中,但在你的条件之前,尝试一下它……如果没有正确输入reCAPCTHA,页面仍然会死亡()我刚刚尝试了你建议的更改,但得到了与之前相同的结果不幸的是,这成功了。我想这可能是评论中唯一一个化学blob所建议的,但我想我之前把它移错了,因为它现在与您的解决方案完美结合
if (isset($_POST['submit'])) {
    $resp = recaptcha_check_answer(...);
    if (!$resp->is_valid) {
        // Wrong captcha
    }
}