Skip to content

Commit c4e3380

Browse files
authored
Merge pull request google#273 from google/clarify-v3-example
Add explanation of sample score
2 parents 822b885 + ddb4f78 commit c4e3380

File tree

3 files changed

+16
-11
lines changed

3 files changed

+16
-11
lines changed

examples/examples.css

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,3 +35,7 @@ main {
3535
display: block;
3636
margin: 1rem;
3737
}
38+
39+
.hidden {
40+
display: none;
41+
}

examples/recaptcha-v3-request-scores.php

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,6 @@
5858
<meta property="og:description" content="reCAPTCHA demo - Request scores" />
5959
<link rel="stylesheet" type="text/css" href="/examples.css">
6060
<title>reCAPTCHA demo - Request scores</title>
61-
6261
<header>
6362
<h1>reCAPTCHA demo</h1><h2>Request scores</h2>
6463
<p><a href="/">↤ Home</a></p>
@@ -73,28 +72,30 @@
7372
else:
7473
// Add the g-recaptcha tag to the form you want to include the reCAPTCHA element
7574
?>
76-
<p>reCAPTCHA will provide a score for this request.</p>
75+
<p>The reCAPTCHA v3 API provides a confidence score for each request.</p>
76+
<p><strong>NOTE:</strong>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 <a href="https://www.google.com/recaptcha/admin" target="_blank">your admin interface</a> and adjust your own threshold accordingly. <strong>Do not raise issues regarding the score you see here.</strong></p>
7777
<ol id="recaptcha-steps">
7878
<li class="step0">reCAPTCHA script loading</li>
79-
<li style="display:none" class="step1"><kbd>grecaptcha.ready()</kbd> fired, calling <pre>grecaptcha.execute('<?php echo $siteKey; ?>', {action: 'homepage'})'</pre></li>
80-
<li style="display:none" class="step2">Received token from reCAPTCHA service, sending to our backend with <kbd>fetch('/recaptcha-v3-verify.php?token='+<span class="token">123</span>)</kbd></li>
81-
<li style="display:none" class="step3">Received response from our backend: <pre class="response">response</pre></li>
79+
<li class="step1 hidden"><kbd>grecaptcha.ready()</kbd> fired, calling <pre>grecaptcha.execute('<?php echo $siteKey; ?>', {action: 'examples/v3scores'})'</pre></li>
80+
<li class="step2 hidden">Received token from reCAPTCHA service, sending to our backend with:
81+
<pre class="token">fetch('/recaptcha-v3-verify.php?token=abc123</pre></li>
82+
<li class="step3 hidden">Received response from our backend: <pre class="response">{"json": "from-backend"}</pre></li>
8283
</ol>
8384
<p><a href="/recaptcha-v3-request-scores.php">⟳ Try again</a></p>
8485

8586
<script src="https://www.google.com/recaptcha/api.js?render=<?php echo $siteKey; ?>"></script>
8687
<script>
8788
const steps = document.getElementById('recaptcha-steps');
8889
grecaptcha.ready(function() {
89-
document.querySelector('.step1').style.display = 'list-item';
90-
grecaptcha.execute('<?php echo $siteKey; ?>', {action: 'homepage'}).then(function(token) {
91-
document.querySelector('.token').innerHTML = token;
92-
document.querySelector('.step2').style.display = 'list-item';
90+
document.querySelector('.step1').classList.remove('hidden');
91+
grecaptcha.execute('<?php echo $siteKey; ?>', {action: 'examples/v3scores'}).then(function(token) {
92+
document.querySelector('.token').innerHTML = 'fetch(\'/recaptcha-v3-verify.php?token=\'' + token;
93+
document.querySelector('.step2').classList.remove('hidden');
9394

9495
fetch('/recaptcha-v3-verify.php?token='+token).then(function(response) {
9596
response.json().then(function(data) {
9697
document.querySelector('.response').innerHTML = JSON.stringify(data, null, 2);
97-
document.querySelector('.step3').style.display = 'list-item';
98+
document.querySelector('.step3').classList.remove('hidden');
9899
});
99100
});
100101
});

examples/recaptcha-v3-verify.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@
4242
// Effectively we're providing an API endpoint here that will accept the token, verify it, and return the action / score to the page
4343
$recaptcha = new \ReCaptcha\ReCaptcha($secret);
4444
$resp = $recaptcha->setExpectedHostname($_SERVER['SERVER_NAME'])
45-
->setExpectedAction('homepage')
45+
->setExpectedAction('examples/v3scores')
4646
->setScoreThreshold(0.5)
4747
->verify($_GET['token'], $_SERVER['REMOTE_ADDR']);
4848
header('Content-type:application/json');

0 commit comments

Comments
 (0)