|
| 1 | +# reCAPTCHA |
| 2 | + |
| 3 | +- [Installation](#installation) |
| 4 | +- [Initialization](#initialization) |
| 5 | +- [Usage](#usage) |
| 6 | +- [Customization](#customization) |
| 7 | + - [Theme](#theme) |
| 8 | + - [Language](#language) |
| 9 | + - [Type](#type) |
| 10 | +- [Full example](#full-example) |
| 11 | + |
| 12 | + |
| 13 | +## Installation |
| 14 | + |
| 15 | +With Composer, add this line to your *require* section : |
| 16 | + |
| 17 | + "phelium/recaptcha": "1.0" |
| 18 | + |
| 19 | +Then run `composer update`. |
| 20 | + |
| 21 | + |
| 22 | +## Initilization |
| 23 | + |
| 24 | + require 'vendor/autoload.php'; |
| 25 | + |
| 26 | + use Phelium\Component\reCAPTCHA; |
| 27 | + |
| 28 | + |
| 29 | +To initialize reCAPTCHA, you must provide your site key and your secret key. |
| 30 | +There is two possible ways : |
| 31 | + |
| 32 | + $reCAPTCHA = new reCAPTCHA('your site key', 'your secret key'); |
| 33 | + |
| 34 | +or |
| 35 | + |
| 36 | + $reCAPTCHA = new reCAPTCHA(); |
| 37 | + $reCAPTCHA->setSiteKey('your site key'); |
| 38 | + $reCAPTCHA->setSecretKey('your secret key'); |
| 39 | + |
| 40 | +## Usage |
| 41 | + |
| 42 | +To generate the *script* tag, use : |
| 43 | + |
| 44 | + $reCAPTCHA->getScript(); |
| 45 | + |
| 46 | +To generate the HTML block, use in your form : |
| 47 | + |
| 48 | + $reCAPTCHA->getHtml(); |
| 49 | + |
| 50 | +Checking the server side, in your form validation script : |
| 51 | + |
| 52 | + if ($reCAPTCHA->isValid($_POST['g-recaptcha-response'])) |
| 53 | + { |
| 54 | + // do whatever you want, the captcha is valid |
| 55 | + } |
| 56 | + |
| 57 | +## Customization |
| 58 | + |
| 59 | +### Theme |
| 60 | + |
| 61 | +Several themes are available : light (default) or dark. |
| 62 | + |
| 63 | + $reCAPTCHA->setTheme('dark'); |
| 64 | + |
| 65 | +### Language |
| 66 | + |
| 67 | +You can change the language of reCAPTCHA. Check [https://developers.google.com/recaptcha/docs/language](https://developers.google.com/recaptcha/docs/language) for more information. |
| 68 | +By default, the language is automatically detected. |
| 69 | + |
| 70 | + $reCAPTCHA->setLanguage('it'); |
| 71 | + |
| 72 | +### Type |
| 73 | + |
| 74 | +Several types are available : image (default) or audio. |
| 75 | + |
| 76 | + $reCAPTCHA->setType('audio'); |
| 77 | + |
| 78 | + |
| 79 | +## Full example |
| 80 | + |
| 81 | +Here is an example : |
| 82 | + |
| 83 | + <?php |
| 84 | + require 'vendor/autoload.php'; |
| 85 | + use Phelium\Component\reCAPTCHA; |
| 86 | + |
| 87 | + $reCAPTCHA = new reCAPTCHA('your site key', 'your secret key'); |
| 88 | + ?> |
| 89 | + |
| 90 | + <html> |
| 91 | + <head> |
| 92 | + <title>reCAPTCHA example</title> |
| 93 | + <?php echo $reCAPTCHA->getScript(); ?> |
| 94 | + </head> |
| 95 | + |
| 96 | + <body> |
| 97 | + |
| 98 | + <?php |
| 99 | + if (isset($_POST['name'])) |
| 100 | + { |
| 101 | + var_dump($_POST); |
| 102 | + |
| 103 | + if ($reCAPTCHA->isValid($_POST['g-recaptcha-response'])) |
| 104 | + { |
| 105 | + echo '<br>-- Captcha OK ! --<br>'; |
| 106 | + } |
| 107 | + } |
| 108 | + ?> |
| 109 | + |
| 110 | + <form action="#" method="POST"> |
| 111 | + <input type="text" name="name" placeholder="name"> |
| 112 | + |
| 113 | + <?php echo $reCAPTCHA->getHtml(); ?> |
| 114 | + |
| 115 | + <input type="submit"> |
| 116 | + </form> |
| 117 | + |
| 118 | + </body> |
| 119 | + </html> |
0 commit comments