NOTE:This is a sample implementation, the score returned here is not a reflection on your Google account or type of traffic. In production, refer to the distribution of scores shown in your admin interface and adjust your own threshold accordingly. Do not raise issues regarding the score you see here.
reCAPTCHA script loading
-
grecaptcha.ready() fired, calling
grecaptcha.execute('', {action: ''})'
+
grecaptcha.ready() fired, calling
grecaptcha.execute('', {action: ''})'
Received token from reCAPTCHA service, sending to our backend with:
fetch('/recaptcha-v3-verify.php?token=abc123
Received response from our backend:
{"json": "from-backend"}
@@ -128,7 +129,7 @@
const steps = document.getElementById('recaptcha-steps');
grecaptcha.ready(function() {
document.querySelector('.step1').classList.remove('hidden');
- grecaptcha.execute('', {action: ''}).then(function(token) {
+ grecaptcha.execute('', {action: ''}).then(function(token) {
document.querySelector('.token').innerHTML = 'fetch(\'/recaptcha-v3-verify.php?action=&token=\'' + token;
document.querySelector('.step2').classList.remove('hidden');
@@ -143,7 +144,7 @@
};
-
+
diff --git a/examples/recaptcha-v2-checkbox-explicit.php b/examples/recaptcha-v2-checkbox-explicit.php
index 1cf637f..3393c68 100644
--- a/examples/recaptcha-v2-checkbox-explicit.php
+++ b/examples/recaptcha-v2-checkbox-explicit.php
@@ -47,7 +47,8 @@
$secret = '';
// Copy the config.php.dist file to config.php and update it with your keys to run the examples
-if ('' == $siteKey && is_readable(__DIR__.'/config.php')) {
+if (is_readable(__DIR__.'/config.php')) {
+ /** @var array{'v2-standard': array{site: string, secret: string}} $config */
$config = include __DIR__.'/config.php';
$siteKey = $config['v2-standard']['site'];
$secret = $config['v2-standard']['secret'];
@@ -91,6 +92,7 @@
setExpectedHostname($_SERVER['SERVER_NAME'])
- ->verify($_POST[ReCaptcha::RESPONSE_KEY], $_SERVER['REMOTE_ADDR'])
+ /** @var string $serverName */
+ $serverName = $_SERVER['SERVER_NAME'];
+
+ /** @var string $responseKey */
+ $responseKey = $_POST[ReCaptcha::RESPONSE_KEY];
+
+ /** @var null|string $remoteAddr */
+ $remoteAddr = $_SERVER['REMOTE_ADDR'];
+
+ $resp = $recaptcha->setExpectedHostname($serverName)
+ ->verify($responseKey, $remoteAddr)
;
if ($resp->isSuccess()) {
@@ -139,7 +150,7 @@
var onloadCallback = function() {
var captchaContainer = document.querySelector('.g-recaptcha');
grecaptcha.render(captchaContainer, {
- 'sitekey' : ''
+ 'sitekey' : ''
});
document.querySelector('button[type="submit"]').disabled = false;
};
diff --git a/examples/recaptcha-v2-checkbox.php b/examples/recaptcha-v2-checkbox.php
index 2031946..25ad3fd 100644
--- a/examples/recaptcha-v2-checkbox.php
+++ b/examples/recaptcha-v2-checkbox.php
@@ -47,7 +47,8 @@
$secret = '';
// Copy the config.php.dist file to config.php and update it with your keys to run the examples
-if ('' == $siteKey && is_readable(__DIR__.'/config.php')) {
+if (is_readable(__DIR__.'/config.php')) {
+ /** @var array{'v2-standard': array{site: string, secret: string}} $config */
$config = include __DIR__.'/config.php';
$siteKey = $config['v2-standard']['site'];
$secret = $config['v2-standard']['secret'];
@@ -91,6 +92,7 @@
setExpectedHostname($_SERVER['SERVER_NAME'])
- ->verify($_POST[ReCaptcha::RESPONSE_KEY], $_SERVER['REMOTE_ADDR'])
+ /** @var string $serverName */
+ $serverName = $_SERVER['SERVER_NAME'];
+
+ /** @var string $responseKey */
+ $responseKey = $_POST[ReCaptcha::RESPONSE_KEY];
+
+ /** @var null|string $remoteAddr */
+ $remoteAddr = $_SERVER['REMOTE_ADDR'];
+
+ $resp = $recaptcha->setExpectedHostname($serverName)
+ ->verify($responseKey, $remoteAddr)
;
if ($resp->isSuccess()) {
// If the response is a success, that's it!
@@ -130,7 +141,7 @@
-
+
diff --git a/examples/recaptcha-v2-invisible.php b/examples/recaptcha-v2-invisible.php
index 2552c55..f075e6b 100644
--- a/examples/recaptcha-v2-invisible.php
+++ b/examples/recaptcha-v2-invisible.php
@@ -47,7 +47,8 @@
$secret = '';
// Copy the config.php.dist file to config.php and update it with your keys to run the examples
-if ('' == $siteKey && is_readable(__DIR__.'/config.php')) {
+if (is_readable(__DIR__.'/config.php')) {
+ /** @var array{'v2-invisible': array{site: string, secret: string}} $config */
$config = include __DIR__.'/config.php';
$siteKey = $config['v2-invisible']['site'];
$secret = $config['v2-invisible']['secret'];
@@ -91,6 +92,7 @@
setExpectedHostname($_SERVER['SERVER_NAME'])
- ->verify($_POST[ReCaptcha::RESPONSE_KEY], $_SERVER['REMOTE_ADDR'])
+ /** @var string $serverName */
+ $serverName = $_SERVER['SERVER_NAME'];
+
+ /** @var string $responseKey */
+ $responseKey = $_POST[ReCaptcha::RESPONSE_KEY];
+
+ /** @var null|string $remoteAddr */
+ $remoteAddr = $_SERVER['REMOTE_ADDR'];
+
+ $resp = $recaptcha->setExpectedHostname($serverName)
+ ->verify($responseKey, $remoteAddr)
;
if ($resp->isSuccess()) {
// If the response is a success, that's it!
@@ -129,7 +140,7 @@
-
+
diff --git a/examples/recaptcha-v3-request-scores.php b/examples/recaptcha-v3-request-scores.php
index d92a7ee..80368b0 100644
--- a/examples/recaptcha-v3-request-scores.php
+++ b/examples/recaptcha-v3-request-scores.php
@@ -44,7 +44,8 @@
$secret = '';
// Copy the config.php.dist file to config.php and update it with your keys to run the examples
-if ('' == $siteKey && is_readable(__DIR__.'/config.php')) {
+if (is_readable(__DIR__.'/config.php')) {
+ /** @var array{v3: array{site: string, secret: string}} $config */
$config = include __DIR__.'/config.php';
$siteKey = $config['v3']['site'];
$secret = $config['v3']['secret'];
@@ -90,18 +91,18 @@
NOTE:This is a sample implementation, the score returned here is not a reflection on your Google account or type of traffic. In production, refer to the distribution of scores shown in your admin interface and adjust your own threshold accordingly. Do not raise issues regarding the score you see here.
reCAPTCHA script loading
-
grecaptcha.ready() fired, calling
grecaptcha.execute('', {action: ''})'
+
grecaptcha.ready() fired, calling
grecaptcha.execute('', {action: ''})'
Received token from reCAPTCHA service, sending to our backend with: