ERROR:Invalid domain for site key

2019-01-27 00:36发布

问题:

I need your help regarding reCaptcha. Recaptcha works well on localhost, but when I try accessing it from a different machine it gives me this error

ERROR:Invalid domain for site key

yet I am using the same keys generated from reCaptcha site

回答1:

If you want to run reCaptcha on localhost you should use secure token like described here: google secure token documentation this solved my own localhost problem. Previously I had an error message "Invalid domain for site key". This may be because nobody says that a localhost has to be named 'localhost' or have the standard IP used for localhosts. Anyways using the secure token solved this completely.

For secure token generation I'm using slushie's php implementation

The PHP part:

<?PHP 

use ReCaptchaSecureToken\ReCaptchaToken as ReCaptchaToken;
require_once("libs/ReCaptchaToken.php");

//Generate recaptcha token
$config = [ 'site_key'      => 'place-your-site-key-here', 
            'site_secret'   => 'place-your-secret-key-here'
            ];
$recaptcha_token = new ReCaptchaToken($config);
$recaptcha_session_id = uniqid('recaptcha');
$recaptcha_secure_token = $recaptcha_token->secureToken($recaptcha_session_id);

?>

The HTML:

<html>
  <head>
  ...
    <script src='//www.google.com/recaptcha/api.js'></script>
  </head>
  <body>
    <form>
    ...
    <div class="g-recaptcha" data-sitekey="place-your-site-key-here" data-stoken="<?PHP echo $recaptcha_secure_token; ?>"></div>
    </form>
  </body>
</html>


回答2:

when I try accessing it from a different machine

If you try to access it from outside, the google (all of the net) sees your external computer IP (IP of your comp in the www) unrelated to 127.162.0.0. So that's why it throws the error.

I recommend you to set reCaptcha into a real web page and register at google.



回答3:

The fix is go to https://www.google.com/recaptcha/admin and register your domain .It will work. Cheers .



标签: recaptcha