【问题标题】:Google Recaptcha V2 'error for site owner: invalid site key'Google Recaptcha V2“网站所有者错误:无效的网站密钥”
【发布时间】:2019-07-28 07:18:08
【问题描述】:

我已尝试在我在另一个网站上使用的以前工作的 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;
}

【问题讨论】:

    标签: php forms recaptcha


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 2020-09-16
      • 1970-01-01
      • 1970-01-01
      • 2020-05-27
      • 1970-01-01
      • 2018-05-04
      • 2015-08-24
      • 1970-01-01
      相关资源
      最近更新 更多