ERROR:Invalid domain for site key

杀马特。学长 韩版系。学妹 提交于 2019-12-18 05:18:05

问题


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 .



来源:https://stackoverflow.com/questions/33604465/errorinvalid-domain-for-site-key

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!