2

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 the reCAPTCHA site.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
bademba
  • 237
  • 4
  • 14

3 Answers3

1

If you want to run reCAPTCHA on localhost you should use a 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 address used for localhosts. Anyway, 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>
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Hexodus
  • 12,361
  • 6
  • 53
  • 72
0

when I try accessing it from a different machine

If you try to access it from outside, Google (all of the Internet) sees your external computer IP address (IP address of your computer on 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.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Igor Savinkin
  • 5,669
  • 8
  • 37
  • 69
  • that could be one solution to it. what i did and solved the problem was to register both ip for server( running localhost) and client(the one i used to access the application from). – bademba Nov 09 '15 at 14:21
  • Igor Savinkin what do you do inorder for the application to be accessed from any machine without adding IP Address of each client accessing the application every now and then – bademba Nov 17 '15 at 11:32
  • @bademba, consider [this post](https://webscraping.pro/insert-configure-recaptcha-no-captcha-code-in-php/) for that. – Igor Savinkin Jul 22 '21 at 08:24
0

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

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Joy Basak
  • 37
  • 4
  • 1
    While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. – pirho Dec 14 '17 at 07:47