Details | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
2 | daniel-mar | 1 | <?php |
2 | /** |
||
3 | * Working sample code to accompany the library. The instructions here assume |
||
4 | * you've just cloned the repo. If you've installed via composer, you will want |
||
5 | * to adjust the path to the autoloader. |
||
6 | * |
||
7 | * 1. Run the server. For example, under Linux you can probably use: |
||
8 | * /usr/bin/php -S "localhost:8000" "examples/example-captcha.php" |
||
9 | * 2. Point your browser at http://localhost:8000 |
||
10 | * 3. Follow the instructions |
||
11 | * |
||
12 | * @copyright Copyright (c) 2015, Google Inc. |
||
13 | * @link http://www.google.com/recaptcha |
||
14 | * |
||
15 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
||
16 | * of this software and associated documentation files (the "Software"), to deal |
||
17 | * in the Software without restriction, including without limitation the rights |
||
18 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
||
19 | * copies of the Software, and to permit persons to whom the Software is |
||
20 | * furnished to do so, subject to the following conditions: |
||
21 | * |
||
22 | * The above copyright notice and this permission notice shall be included in |
||
23 | * all copies or substantial portions of the Software. |
||
24 | * |
||
25 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
||
26 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
||
27 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
||
28 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
||
29 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
||
30 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
||
31 | * THE SOFTWARE. |
||
32 | */ |
||
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 | |||
38 | // Register API keys at https://www.google.com/recaptcha/admin |
||
39 | $siteKey = ''; |
||
40 | $secret = ''; |
||
41 | |||
42 | // reCAPTCHA supported 40+ languages listed here: https://developers.google.com/recaptcha/docs/language |
||
43 | $lang = 'en'; |
||
44 | ?> |
||
45 | <html> |
||
46 | <head> |
||
47 | <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/> |
||
48 | <title>reCAPTCHA Example</title> |
||
49 | <link rel="shortcut icon" href="//www.gstatic.com/recaptcha/admin/favicon.ico" type="image/x-icon"/> |
||
50 | <style type="text/css"> |
||
51 | body { |
||
52 | margin: 1em 5em 0 5em; |
||
53 | font-family: sans-serif; |
||
54 | } |
||
55 | fieldset { |
||
56 | display: inline; |
||
57 | padding: 1em; |
||
58 | } |
||
59 | </style> |
||
60 | </head> |
||
61 | <body> |
||
62 | <h1>reCAPTCHA Example</h1> |
||
63 | <?php if ($siteKey === '' || $secret === ''): ?> |
||
64 | <h2>Add your keys</h2> |
||
65 | <p>If you do not have keys already then visit <kbd> |
||
66 | <a href = "https://www.google.com/recaptcha/admin"> |
||
67 | https://www.google.com/recaptcha/admin</a></kbd> to generate them. |
||
68 | Edit this file and set the respective keys in <kbd>$siteKey</kbd> and |
||
69 | <kbd>$secret</kbd>. Reload the page after this.</p> |
||
70 | <?php |
||
71 | elseif (isset($_POST['g-recaptcha-response'])): |
||
72 | // The POST data here is unfiltered because this is an example. |
||
73 | // In production, *always* sanitise and validate your input' |
||
74 | ?> |
||
75 | <h2><kbd>POST</kbd> data</h2> |
||
76 | <kbd><pre><?php var_export($_POST); ?></pre></kbd> |
||
77 | <?php |
||
78 | // If the form submission includes the "g-captcha-response" field |
||
79 | // Create an instance of the service using your secret |
||
80 | $recaptcha = new \ReCaptcha\ReCaptcha($secret); |
||
81 | |||
82 | // If file_get_contents() is locked down on your PHP installation to disallow |
||
83 | // its use with URLs, then you can use the alternative request method instead. |
||
84 | // This makes use of fsockopen() instead. |
||
85 | // $recaptcha = new \ReCaptcha\ReCaptcha($secret, new \ReCaptcha\RequestMethod\SocketPost()); |
||
86 | |||
87 | // Make the call to verify the response and also pass the user's IP address |
||
88 | $resp = $recaptcha->verify($_POST['g-recaptcha-response'], $_SERVER['REMOTE_ADDR']); |
||
89 | |||
90 | if ($resp->isSuccess()): |
||
91 | // If the response is a success, that's it! |
||
92 | ?> |
||
93 | <h2>Success!</h2> |
||
94 | <p>That's it. Everything is working. Go integrate this into your real project.</p> |
||
95 | <p><a href="/">Try again</a></p> |
||
96 | <?php |
||
97 | else: |
||
98 | // If it's not successful, then one or more error codes will be returned. |
||
99 | ?> |
||
100 | <h2>Something went wrong</h2> |
||
101 | <p>The following error was returned: <?php |
||
102 | foreach ($resp->getErrorCodes() as $code) { |
||
103 | echo '<kbd>' , $code , '</kbd> '; |
||
104 | } |
||
105 | ?></p> |
||
106 | <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>. |
||
107 | <p><strong>Note:</strong> Error code <kbd>missing-input-response</kbd> may mean the user just didn't complete the reCAPTCHA.</p> |
||
108 | <p><a href="/">Try again</a></p> |
||
109 | <?php |
||
110 | endif; |
||
111 | else: |
||
112 | // Add the g-recaptcha tag to the form you want to include the reCAPTCHA element |
||
113 | ?> |
||
114 | <p>Complete the reCAPTCHA then submit the form.</p> |
||
115 | <form action="/" method="post"> |
||
116 | <fieldset> |
||
117 | <legend>An example form</legend> |
||
118 | <p>Example input A: <input type="text" name="ex-a" value="foo"></p> |
||
119 | <p>Example input B: <input type="text" name="ex-b" value="bar"></p> |
||
120 | |||
121 | <div class="g-recaptcha" data-sitekey="<?php echo $siteKey; ?>"></div> |
||
122 | <script type="text/javascript" |
||
123 | src="https://www.google.com/recaptcha/api.js?hl=<?php echo $lang; ?>"> |
||
124 | </script> |
||
125 | <p><input type="submit" value="Submit" /></p> |
||
126 | </fieldset> |
||
127 | </form> |
||
128 | <?php endif; ?> |
||
129 | </body> |
||
130 | </html> |