recaptcha-v3-request-scores.php 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. <?php
  2. /**
  3. * BSD 3-Clause License
  4. * @copyright (c) 2019, Google Inc.
  5. * @link https://www.google.com/recaptcha
  6. * All rights reserved.
  7. *
  8. * Redistribution and use in source and binary forms, with or without
  9. * modification, are permitted provided that the following conditions are met:
  10. * 1. Redistributions of source code must retain the above copyright notice, this
  11. * list of conditions and the following disclaimer.
  12. *
  13. * 2. Redistributions in binary form must reproduce the above copyright notice,
  14. * this list of conditions and the following disclaimer in the documentation
  15. * and/or other materials provided with the distribution.
  16. *
  17. * 3. Neither the name of the copyright holder nor the names of its
  18. * contributors may be used to endorse or promote products derived from
  19. * this software without specific prior written permission.
  20. *
  21. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  22. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  23. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  24. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  25. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  26. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  27. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  28. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  29. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  30. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  31. */
  32. require __DIR__ . '/appengine-https.php';
  33. // Initiate the autoloader. The file should be generated by Composer.
  34. // You will provide your own autoloader or require the files directly if you did
  35. // not install via Composer.
  36. require_once __DIR__ . '/../vendor/autoload.php';
  37. // Register API keys at https://www.google.com/recaptcha/admin
  38. $siteKey = '';
  39. $secret = '';
  40. // Copy the config.php.dist file to config.php and update it with your keys to run the examples
  41. if ($siteKey == '' && is_readable(__DIR__ . '/config.php')) {
  42. $config = include __DIR__ . '/config.php';
  43. $siteKey = $config['v3']['site'];
  44. $secret = $config['v3']['secret'];
  45. }
  46. // reCAPTCHA supports 40+ languages listed here: https://developers.google.com/recaptcha/docs/language
  47. $lang = 'en';
  48. // The v3 API lets you provide some context for the check by specifying an action.
  49. // See: https://developers.google.com/recaptcha/docs/v3
  50. $pageAction = 'examples/v3scores';
  51. ?>
  52. <!DOCTYPE html>
  53. <html lang="en">
  54. <meta charset="UTF-8">
  55. <meta name="viewport" content="width=device-width,height=device-height,minimum-scale=1">
  56. <link rel="shortcut icon" href="https://www.gstatic.com/recaptcha/admin/favicon.ico" type="image/x-icon"/>
  57. <link rel="canonical" href="https://recaptcha-demo.appspot.com/recaptcha-v3-request-scores.php">
  58. <script type="application/ld+json">{ "@context": "http://schema.org", "@type": "WebSite", "name": "reCAPTCHA demo - Request scores", "url": "https://recaptcha-demo.appspot.com/recaptcha-v3-request-scores.php" }</script>
  59. <meta name="description" content="reCAPTCHA demo - Request scores" />
  60. <meta property="og:url" content="https://recaptcha-demo.appspot.com/recaptcha-v3-request-scores.php" />
  61. <meta property="og:type" content="website" />
  62. <meta property="og:title" content="reCAPTCHA demo - Request scores" />
  63. <meta property="og:description" content="reCAPTCHA demo - Request scores" />
  64. <link rel="stylesheet" type="text/css" href="/examples.css">
  65. <title>reCAPTCHA demo - Request scores</title>
  66. <header>
  67. <h1>reCAPTCHA demo</h1><h2>Request scores</h2>
  68. <p><a href="/">↩️ Home</a></p>
  69. </header>
  70. <main>
  71. <?php
  72. if ($siteKey === '' || $secret === ''):
  73. ?>
  74. <h2>Add your keys</h2>
  75. <p>If you do not have keys already then visit <kbd> <a href = "https://www.google.com/recaptcha/admin">https://www.google.com/recaptcha/admin</a></kbd> to generate them. Edit this file and set the respective keys in <kbd>$siteKey</kbd> and <kbd>$secret</kbd>. Reload the page after this.</p>
  76. <?php
  77. else:
  78. // Add the g-recaptcha tag to the form you want to include the reCAPTCHA element
  79. ?>
  80. <p>The reCAPTCHA v3 API provides a confidence score for each request.</p>
  81. <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>
  82. <ol id="recaptcha-steps">
  83. <li class="step0">reCAPTCHA script loading</li>
  84. <li class="step1 hidden"><kbd>grecaptcha.ready()</kbd> fired, calling <pre>grecaptcha.execute('<?php echo $siteKey; ?>', {action: '<?php echo $pageAction; ?>'})'</pre></li>
  85. <li class="step2 hidden">Received token from reCAPTCHA service, sending to our backend with:
  86. <pre class="token">fetch('/recaptcha-v3-verify.php?token=abc123</pre></li>
  87. <li class="step3 hidden">Received response from our backend: <pre class="response">{"json": "from-backend"}</pre></li>
  88. </ol>
  89. <p><a href="/recaptcha-v3-request-scores.php">⤴️ Try again</a></p>
  90. <script src="https://www.google.com/recaptcha/api.js?render=<?php echo $siteKey; ?>"></script>
  91. <script>
  92. const steps = document.getElementById('recaptcha-steps');
  93. grecaptcha.ready(function() {
  94. document.querySelector('.step1').classList.remove('hidden');
  95. grecaptcha.execute('<?php echo $siteKey; ?>', {action: '<?php echo $pageAction; ?>'}).then(function(token) {
  96. document.querySelector('.token').innerHTML = 'fetch(\'/recaptcha-v3-verify.php?action=<?php echo $pageAction; ?>&token=\'' + token;
  97. document.querySelector('.step2').classList.remove('hidden');
  98. fetch('/recaptcha-v3-verify.php?action=<?php echo $pageAction; ?>&token='+token).then(function(response) {
  99. response.json().then(function(data) {
  100. document.querySelector('.response').innerHTML = JSON.stringify(data, null, 2);
  101. document.querySelector('.step3').classList.remove('hidden');
  102. });
  103. });
  104. });
  105. });
  106. </script>
  107. <?php
  108. endif;?>
  109. </main>
  110. <!-- Google Analytics - just ignore this -->
  111. <script async src="https://www.googletagmanager.com/gtag/js?id=UA-123057962-1"></script>
  112. <script>window.dataLayer = window.dataLayer || []; function gtag(){dataLayer.push(arguments);} gtag('js', new Date()); gtag('config', 'UA-123057962-1');</script>