Reputation: 247
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.
Upvotes: 2
Views: 9754
Reputation: 37
The fix is go to https://www.google.com/recaptcha/admin and register your domain. It will work.
Upvotes: 0
Reputation: 12955
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>
Upvotes: 1
Reputation: 6277
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.
Upvotes: 0