簡體   English   中英

Recaptcha V2.0始終返回bool(false)

[英]Recaptcha V2.0 always returns bool(false)

我對Recaptcha V2.0總是返回false或bool(false)有問題。 感覺好像沒有通過正確的密鑰進行驗證,但是我已經四倍地檢查了我添加的域和我在代碼中使用的密鑰。 也許我只是做了一些錯誤的代碼。

HTML形式:

<form method="post" action="/php/emailCode.php">
        Your Name <label><input type="text" name="name"></label>
        <br/>
        Email Address <label><input type="text" name="email"></label>
        <br/>
        Message <label><textarea name="message"></textarea></label>
        <br />
        <div id="captcha" data-sitekey="xxxxxxxxxxxxxxxxxxxxxxx"></div>
        <br />
        <input id="submitButton" type="submit">
    </form>
    <script src="https://www.google.com/recaptcha/api.js?onload=onloadCallback&render=explicit" async defer></script>

PHP:

<?php
if(isset($_POST['g-recaptcha-response']) && $_POST['g-recaptcha-response']){
    $secret = "xxxxxxxxxxxxxxxxxx";
    $ip = $_SERVER['REMOTE_ADDR'];
    $captcha = $_POST['g-recaptcha-response'];
    $rsp  = file_get_contents("https://www.google.com/recaptcha/api/siteverify?secret=" . $secret . "&response=" . $captcha . "&remoteip=" . $ip);

    var_dump($rsp);
    $arr = json_decode($rsp);
    if($arr->success === true){
        $EmailFrom = "example@example.com";
        $EmailTo = "example@gmail.com";
        $Subject = $_POST['email'];
        $Name = $_POST['name'];
        $Message = $_POST['message'];

        // prepare email body text
        $Body = "";
        $Body .= "Name: ";
        $Body .= $Name;
        $Body .= "\n";
        $Body .= "Message: ";
        $Body .= $Message;
        $Body .= "\n";

        // send email
        $success = mail($EmailTo, $Subject, $Body, "From: <$EmailFrom>");
    }else{
        echo 'Failure';
    }

}

同樣在我的頭上運行:

<script type="text/javascript">
    var onloadCallback = function() {
        grecaptcha.render('captcha', {
            'sitekey' : 'xxxxxxxxxxxxxxxxxxxxxx'
        });
    };
</script>

我真的不知道為什么它會返回false。 提前謝謝你的幫助!

在Recaptcha中,您有兩個鍵:

網站密鑰在您的網站提供給用戶的HTML代碼中使用此代碼。

密鑰用於在您的網站和Google之間進行通信。 一定要保密。

因此,在我的情況下,問題是由於某種原因,我在客戶端和服務器端都使用了一個密鑰,這是錯誤的。 您有一個用於html表單的密鑰(客戶端)(這是“站點密鑰”),而另一個具有服務器端的密鑰。 (這是密鑰)

轉到https://www.google.com/recaptcha/admin#list並單擊您的站點,然后閱讀“ 將reCAPTCHA添加到您的站點

編輯:在您的客戶端:

  .
  ..
  ...
  <div id="captcha" data-sitekey="SITE KEY"></div>
  ...
  ..
  .

在服務器端:

.
..
...
$secret = "SECRET KEY";
...
..
.
  <?php include_once "recaptchalib.php";  ?> //library to be added
<form action='' method=''>
<input type="text" id="myphn" placeholder="Email/Username">
        <input type="Password" id="mypass" placeholder="Password">
        <div class="g-recaptcha" data-sitekey="6Ldd7AoUAAAAAGMNn1YkptZO7s9TY2xHe7nW45ma" id='recaptcha'></div>
//contains the key   
<input type='submit'>
</form>

 $response=$_POST['g-recaptcha-response']
    $secret = '6Ldd7AoUAAAAAGpSbJYqM9Tsh04NG_1vCPeRlFOe';

            $rsp=file_get_contents("https://www.google.com/recaptcha/api/siteverify?secret=$secret&response=$response");
            $arr= json_decode($rsp,TRUE);
            if($arr['success'] == TRUE){
            //var_dump($rsp); die;
}else{ echo 'not done';
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM