Google Recaptcha V2“网站所有者错误:无效的网站密钥”

时间:2019-03-06 15:07:35

标签: php forms recaptcha

我曾尝试在另一个网站上使用的先前工作的Google Recaptcha表格上删除并获取新的网站密钥。

无论我做什么(我已将脚本移到头部等),都无法使Recaptcha正常工作。

有2个文件(form.php)是在wordpress中调用html的模板文件,而(form_process.php)是在后台提交表单的文件。我完全迷失了如何获取错误代码“网站所有者错误:无效的网站密钥”以停止显示和验证码工作

预先感谢

Form.php>

<article id='contact-page'>    
<section class='section-class' data-section-name='Contact'>
    <?php include ('form_process.php');?>        
    <form id="contact" method="post" >
        <div id="column-flex-left-301">
            <div class="image-spacer"></div>
                <h1 class="">Contact</h1>
                <fieldset id="field-no-ui">
                    <input placeholder="Your name" type="text" tabindex="1" name="name1" value="<?= $name ?>" >
                </fieldset>
                <span class="error"><?= $name_error ?></span>
                <fieldset id="field-no-ui">
                <input placeholder="Your Email Address" type="text" name="email" value="<?= $email ?>" tabindex="2" >
                </fieldset>                <div class="image-spacer"></div>
                <span class="error"><?= $email_error ?></span>
        </div>
        <div id="column-flex-right-301">
                <fieldset id="field-no-ui">
                    <textarea id="field-no-ui" class="msg-area" placeholder="Type your Message Here...." name="message" value="<?= $message ?>" tabindex="3" ></textarea>
                </fieldset>
                <div class="g-recaptcha" data-sitekey="xxx"></div>
                <span class="captcha-failed"><?= $captchafailed; ?></span>
                <span class="sent"><?= $sent; ?></span>
                <fieldset id="field-no-ui-submit">
                <button name="submit" type="submit" id="contact-submit" data-submit="...Sending">Submit</button>
                </fieldset>
            <script src="https://www.google.com/recaptcha/api.js" async defer></script>
        </div>
    </form>
</section>
</article>

和form_process.php>

<?php
function post_captcha($user_response) {
$fields_string = '';
$fields = array(
    'secret' => 'xxx',
    'response' => $user_response
);
foreach($fields as $key=>$value)
    $fields_string .= $key . '=' . $value . '&';
$fields_string = rtrim($fields_string, '&');
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 
'https://www.google.com/recaptcha/api/siteverify');
curl_setopt($ch, CURLOPT_POST, count($fields));
curl_setopt($ch, CURLOPT_POSTFIELDS, $fields_string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, True);
$result = curl_exec($ch);
curl_close($ch);
return json_decode($result, true);
}
$res = post_captcha($_POST['g-recaptcha-response']);
$name_error = $email_error = $captchafailed = "";
$name = $email = $message = $sent = "";
if (isset($_POST['submit']) AND (!$res['success'])) {    
    $captchafailed = "please check reCaptcha";
}
if ($_SERVER["REQUEST_METHOD"] == "POST") {
if (empty($_POST["name1"])) {
    $name_error = "Name is required";
} else {
    $name = test_input($_POST["name1"]);
    if (!preg_match("/^[a-zA-Z ]*$/",$name)) {
        $name_error = "Only letters and white space allowed";
    }
}
if (empty($_POST["email"])) {
    $email_error = "Email is required";
} else {
    $email = test_input($_POST["email"]);
    if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
        $email_error = "Invalid email format";
    }
}
if (empty($_POST["message"])) {
    $message = "";
} else {
    $message = test_input($_POST["message"]);
}
if ($name_error == '' and $email_error == '' and ($res['success']) ){
    $message_body = '';
    unset($_POST['submit']);
    foreach ($_POST as $key => $value){
        $message_body .=  "$key: $value\n";
    }
    $email = $_POST['email'];
    $to = 'xxx';
    $subject = 'Contact Form Submit';
    $headers = 'From:' . $email . "\n" . 'Reply-to: ' . $email . "\n"  ;
    if (mail($to, $subject, $message, $headers)) {
        $sent = "Message sent";
        $name = $email = $message = '';
    }
}    
}
function test_input($data) {
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}

1 个答案:

答案 0 :(得分:0)

问题解决了,我已经在front-page.php上写了Recaptcha的html,并且没有调用旧的form.php模板,因此站点密钥在html中有所不同