recaptcha-v2-checkbox.php 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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['v2-standard']['site'];
  44. $secret = $config['v2-standard']['secret'];
  45. }
  46. // reCAPTCHA supports 40+ languages listed here: https://developers.google.com/recaptcha/docs/language
  47. $lang = 'en';
  48. ?>
  49. <!DOCTYPE html>
  50. <html lang="en">
  51. <meta charset="UTF-8">
  52. <meta name="viewport" content="width=device-width,height=device-height,minimum-scale=1">
  53. <link rel="shortcut icon" href="https://www.gstatic.com/recaptcha/admin/favicon.ico" type="image/x-icon"/>
  54. <link rel="canonical" href="https://recaptcha-demo.appspot.com/recaptcha-v2-checkbox.php">
  55. <script type="application/ld+json">{ "@context": "http://schema.org", "@type": "WebSite", "name": "reCAPTCHA demo - \"I'm not a robot\" checkbox", "url": "https://recaptcha-demo.appspot.com/recaptcha-v2-checkbox.php" }</script>
  56. <meta name="description" content="reCAPTCHA demo - &quot;I'm not a robot&quot; checkbox" />
  57. <meta property="og:url" content="https://recaptcha-demo.appspot.com/recaptcha-v2-checkbox.php" />
  58. <meta property="og:type" content="website" />
  59. <meta property="og:title" content="reCAPTCHA demo - &quot;I'm not a robot&quot; checkbox" />
  60. <meta property="og:description" content="reCAPTCHA demo - &quot;I'm not a robot&quot; checkbox" />
  61. <link rel="stylesheet" type="text/css" href="/examples.css">
  62. <title>reCAPTCHA demo - "I'm not a robot" checkbox</title>
  63. <header>
  64. <h1>reCAPTCHA demo</h1><h2>"I'm not a robot" checkbox</h2>
  65. <p><a href="/">↩️ Home</a></p>
  66. </header>
  67. <main>
  68. <?php
  69. if ($siteKey === '' || $secret === ''):
  70. ?>
  71. <h2>Add your keys</h2>
  72. <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 the <kbd>config.php</kbd> file or directly to <kbd>$siteKey</kbd> and <kbd>$secret</kbd>. Reload the page after this.</p>
  73. <?php
  74. elseif (isset($_POST['g-recaptcha-response'])):
  75. // The POST data here is unfiltered because this is an example.
  76. // In production, *always* sanitise and validate your input'
  77. ?>
  78. <h2><kbd>POST</kbd> data</h2>
  79. <kbd><pre><?php var_export($_POST);?></pre></kbd>
  80. <?php
  81. // If the form submission includes the "g-captcha-response" field
  82. // Create an instance of the service using your secret
  83. $recaptcha = new \ReCaptcha\ReCaptcha($secret);
  84. // If file_get_contents() is locked down on your PHP installation to disallow
  85. // its use with URLs, then you can use the alternative request method instead.
  86. // This makes use of fsockopen() instead.
  87. // $recaptcha = new \ReCaptcha\ReCaptcha($secret, new \ReCaptcha\RequestMethod\SocketPost());
  88. // Make the call to verify the response and also pass the user's IP address
  89. $resp = $recaptcha->setExpectedHostname($_SERVER['SERVER_NAME'])
  90. ->verify($_POST['g-recaptcha-response'], $_SERVER['REMOTE_ADDR']);
  91. if ($resp->isSuccess()):
  92. // If the response is a success, that's it!
  93. ?>
  94. <h2>Success!</h2>
  95. <kbd><pre><?php var_export($resp);?></pre></kbd>
  96. <p>That's it. Everything is working. Go integrate this into your real project.</p>
  97. <p><a href="/recaptcha-v2-checkbox.php">⤴️ Try again</a></p>
  98. <?php
  99. else:
  100. // If it's not successful, then one or more error codes will be returned.
  101. ?>
  102. <h2>Something went wrong</h2>
  103. <kbd><pre><?php var_export($resp);?></pre></kbd>
  104. <p>Check the error code reference at <kbd><a href="https://developers.google.com/recaptcha/docs/verify#error-code-reference">https://developers.google.com/recaptcha/docs/verify#error-code-reference</a></kbd>.
  105. <p><strong>Note:</strong> Error code <kbd>missing-input-response</kbd> may mean the user just didn't complete the reCAPTCHA.</p>
  106. <p><a href="/recaptcha-v2-checkbox.php">⤴️ Try again</a></p>
  107. <?php
  108. endif;
  109. else:
  110. // Add the g-recaptcha tag to the form you want to include the reCAPTCHA element
  111. ?>
  112. <p>Complete the reCAPTCHA then submit the form.</p>
  113. <form action="/recaptcha-v2-checkbox.php" method="post">
  114. <fieldset>
  115. <legend>An example form</legend>
  116. <label class="form-field">Example input A: <input type="text" name="ex-a" value="foo"></label>
  117. <label class="form-field">Example input B: <input type="text" name="ex-b" value="bar"></label>
  118. <!-- Default behaviour looks for the g-recaptcha class with a data-sitekey attribute -->
  119. <div class="g-recaptcha form-field" data-sitekey="<?php echo $siteKey; ?>"></div>
  120. <!-- Submitting before the widget loads will result in a missing-input-response error so you need to verify server side -->
  121. <button class="form-field" type="submit">Submit ↦</button>
  122. </fieldset>
  123. </form>
  124. <script type="text/javascript" src="https://www.google.com/recaptcha/api.js?hl=<?php echo $lang; ?>"></script>
  125. <?php
  126. endif;?>
  127. </main>
  128. <!-- Google Analytics - just ignore this -->
  129. <script async src="https://www.googletagmanager.com/gtag/js?id=UA-123057962-1"></script>
  130. <script>window.dataLayer = window.dataLayer || []; function gtag(){dataLayer.push(arguments);} gtag('js', new Date()); gtag('config', 'UA-123057962-1');</script>