|
1 | 1 | # reCAPTCHA PHP client library
|
2 | 2 |
|
3 | 3 | [](https://travis-ci.org/google/recaptcha)
|
| 4 | +[](https://coveralls.io/github/google/recaptcha) |
4 | 5 | [](https://packagist.org/packages/google/recaptcha)
|
5 | 6 | [](https://packagist.org/packages/google/recaptcha)
|
6 | 7 |
|
7 |
| -* Project page: http://www.google.com/recaptcha/ |
8 |
| -* Repository: https://github.com/google/recaptcha |
9 |
| -* Version: 1.1.3 |
10 |
| -* License: BSD, see [LICENSE](LICENSE) |
11 |
| - |
12 |
| -## Description |
13 |
| - |
14 | 8 | reCAPTCHA is a free CAPTCHA service that protect websites from spam and abuse.
|
15 |
| -This is Google authored code that provides plugins for third-party integration |
16 |
| -with reCAPTCHA. |
| 9 | +This is a PHP library that wraps up the server-side verification step required |
| 10 | +to process responses from the reCAPTCHA service. This client supports both v2 |
| 11 | +and v3. |
| 12 | + |
| 13 | +- reCAPTCHA: https://www.google.com/recaptcha |
| 14 | +- This repo: https://github.com/google/recaptcha |
| 15 | +- Version: 1.2 |
| 16 | +- License: BSD, see [LICENSE](LICENSE) |
17 | 17 |
|
18 | 18 | ## Installation
|
19 | 19 |
|
20 |
| -### Composer (Recommended) |
| 20 | +### Composer (recommended) |
21 | 21 |
|
22 |
| -[Composer](https://getcomposer.org/) is a widely used dependency manager for PHP |
23 |
| -packages. This reCAPTCHA client is available on Packagist as |
24 |
| -[`google/recaptcha`](https://packagist.org/packages/google/recaptcha) and can be |
25 |
| -installed either by running the `composer require` command or adding the library |
26 |
| -to your `composer.json`. To enable Composer for you project, refer to the |
27 |
| -project's [Getting Started](https://getcomposer.org/doc/00-intro.md) |
28 |
| -documentation. |
| 22 | +Use [Composer](https://getcomposer.org) to install this library from Packagist: |
| 23 | +[`google/recaptcha`](https://packagist.org/packages/google/recaptcha) |
29 | 24 |
|
30 |
| -To add this dependency using the command, run the following from within your |
31 |
| -project directory: |
32 |
| -``` |
33 |
| -composer require google/recaptcha "~1.1" |
| 25 | +Run the following command from your project directory to add the dependency: |
| 26 | + |
| 27 | +```sh |
| 28 | +composer require google/recaptcha "^1.2" |
34 | 29 | ```
|
35 | 30 |
|
36 | 31 | Alternatively, add the dependency directly to your `composer.json` file:
|
| 32 | + |
37 | 33 | ```json
|
38 | 34 | "require": {
|
39 |
| - "google/recaptcha": "~1.1" |
| 35 | + "google/recaptcha": "^1.2" |
40 | 36 | }
|
41 | 37 | ```
|
42 | 38 |
|
43 |
| -### Direct download (no Composer) |
| 39 | +### Direct download |
44 | 40 |
|
45 |
| -If you wish to install the library manually (i.e. without Composer), then you |
46 |
| -can use the links on the main project page to either clone the repo or download |
47 |
| -the [ZIP file](https://github.com/google/recaptcha/archive/master.zip). For |
48 |
| -convenience, an autoloader script is provided in `src/autoload.php` which you |
49 |
| -can require into your script instead of Composer's `vendor/autoload.php`. For |
50 |
| -example: |
| 41 | +Download the [ZIP file](https://github.com/google/recaptcha/archive/master.zip) |
| 42 | +and extract into your project. An autoloader script is provided in |
| 43 | +`src/autoload.php` which you can require into your script. For example: |
51 | 44 |
|
52 | 45 | ```php
|
53 |
| -require('/path/to/recaptcha/src/autoload.php'); |
| 46 | +require_once '/path/to/recaptcha/src/autoload.php'; |
54 | 47 | $recaptcha = new \ReCaptcha\ReCaptcha($secret);
|
55 | 48 | ```
|
56 | 49 |
|
57 | 50 | The classes in the project are structured according to the
|
58 |
| -[PSR-4](http://www.php-fig.org/psr/psr-4/) standard, so you may of course also |
59 |
| -use your own autoloader or require the needed files directly in your code. |
| 51 | +[PSR-4](http://www.php-fig.org/psr/psr-4/) standard, so you can also use your |
| 52 | +own autoloader or require the needed files directly in your code. |
60 | 53 |
|
61 |
| -### Development install |
| 54 | +## Usage |
62 | 55 |
|
63 |
| -If you would like to contribute to this project or run the unit tests on within |
64 |
| -your own environment you will need to install the development dependencies, in |
65 |
| -this case that means [PHPUnit](https://phpunit.de/). If you clone the repo and |
66 |
| -run `composer install` from within the repo, this will also grab PHPUnit and all |
67 |
| -its dependencies for you. If you only need the autoloader installed, then you |
68 |
| -can always specify to Composer not to run in development mode, e.g. `composer |
69 |
| -install --no-dev`. |
| 56 | +First obtain the appropriate keys for the type of reCAPTCHA you wish to |
| 57 | +integrate for v2 at https://www.google.com/recaptcha/admin or v3 at |
| 58 | +https://g.co/recaptcha/v3. |
70 | 59 |
|
71 |
| -*Note:* These dependencies are only required for development, there's no |
72 |
| -requirement for them to be included in your production code. |
| 60 | +Then follow the [integration guide on the developer |
| 61 | +site](https://developers.google.com/recaptcha/intro) to add the reCAPTCHA |
| 62 | +functionality into your frontend. |
73 | 63 |
|
74 |
| -## Usage |
| 64 | +This library comes in when you need to verify the user's response. On the PHP |
| 65 | +side you need the response from the reCAPTCHA service and secret key from your |
| 66 | +credentials. Instantiate the `ReCaptcha` class with your secret key, specify any |
| 67 | +additional validation rules, and then call `verify()` with the reCAPTCHA |
| 68 | +response and user's IP address. For example: |
75 | 69 |
|
76 |
| -First, register keys for your site at https://www.google.com/recaptcha/admin |
| 70 | +```php |
| 71 | +<?php |
| 72 | +$recaptcha = new \ReCaptcha\ReCaptcha($secret); |
| 73 | +$resp = $recaptcha->setExpectedHostname('recaptcha-demo.appspot.com') |
| 74 | + ->verify($gRecaptchaResponse, $remoteIp); |
| 75 | +if ($resp->isSuccess()) { |
| 76 | + // Verified! |
| 77 | +} else { |
| 78 | + $errors = $resp->getErrorCodes(); |
| 79 | +} |
| 80 | +``` |
| 81 | + |
| 82 | +The following methods are available: |
| 83 | + |
| 84 | +- `setExpectedHostname($hostname)`: ensures the hostname matches. You must do |
| 85 | + this if you have disabled "Domain/Package Name Validation" for your |
| 86 | + credentials. |
| 87 | +- `setExpectedApkPackageName($apkPackageName)`: if you're verifying a response |
| 88 | + from an Android app. Again, you must do this if you have disabled |
| 89 | + "Domain/Package Name Validation" for your credentials. |
| 90 | +- `setExpectedAction($action)`: ensures the action matches for the v3 API. |
| 91 | +- `setScoreThreshold($threshold)`: set a score theshold for responses from the |
| 92 | + v3 API |
| 93 | +- `setChallengeTimeout($timeoutSeconds)`: set a timeout between the user passing |
| 94 | + the reCAPTCHA and your server processing it. |
| 95 | + |
| 96 | +Each of the `set`\*`()` methods return the `ReCaptcha` instance so you can chain |
| 97 | +them together. For example: |
77 | 98 |
|
78 |
| -When your app receives a form submission containing the `g-recaptcha-response` |
79 |
| -field, you can verify it using: |
80 | 99 | ```php
|
81 | 100 | <?php
|
82 | 101 | $recaptcha = new \ReCaptcha\ReCaptcha($secret);
|
83 |
| -$resp = $recaptcha->verify($gRecaptchaResponse, $remoteIp); |
| 102 | +$resp = $recaptcha->setExpectedHostname('recaptcha-demo.appspot.com') |
| 103 | + ->setExpectedAction('homepage') |
| 104 | + ->setScoreThreshold(0.5) |
| 105 | + ->verify($gRecaptchaResponse, $remoteIp); |
| 106 | + |
84 | 107 | if ($resp->isSuccess()) {
|
85 |
| - // verified! |
86 |
| - // if Domain Name Validation turned off don't forget to check hostname field |
87 |
| - // if($resp->getHostName() === $_SERVER['SERVER_NAME']) { } |
| 108 | + // Verified! |
88 | 109 | } else {
|
89 | 110 | $errors = $resp->getErrorCodes();
|
90 | 111 | }
|
91 | 112 | ```
|
92 | 113 |
|
93 |
| -You can see an end-to-end working example in |
94 |
| -[examples/example-captcha.php](examples/example-captcha.php) |
| 114 | +You can find the constants for the libraries error codes in the `ReCaptcha` |
| 115 | +class constants, e.g. `ReCaptcha::E_HOSTNAME_MISMATCH` |
| 116 | + |
| 117 | +For more details on usage and structure, see [ARCHITECTURE](ARCHITECTURE.md). |
95 | 118 |
|
96 |
| -## Upgrading |
| 119 | +### Examples |
97 | 120 |
|
98 |
| -### From 1.0.0 |
| 121 | +You can see examples of each reCAPTCHA type in [examples/](examples/). You can |
| 122 | +run the examples locally by using the Composer script: |
| 123 | + |
| 124 | +```sh |
| 125 | +composer run-script serve-examples |
| 126 | +``` |
99 | 127 |
|
100 |
| -The previous version of this client is still available on the `1.0.0` tag [in |
101 |
| -this repo](https://github.com/google/recaptcha/tree/1.0.0) but it is purely for |
102 |
| -reference and will not receive any updates. |
| 128 | +This makes use of the in-built PHP dev server to host the examples at |
| 129 | +http://localhost:8080/ |
103 | 130 |
|
104 |
| -The major changes in 1.1.0 are: |
105 |
| -* installation now via Composer; |
106 |
| -* class loading also via Composer; |
107 |
| -* classes now namespaced; |
108 |
| -* old method call was `$rc->verifyResponse($remoteIp, $response)`, new call is |
109 |
| - `$rc->verify($response, $remoteIp)` |
| 131 | +These are also hosted on Google AppEngine Flexible environment at |
| 132 | +https://recaptcha-demo.appspot.com/. This is configured by |
| 133 | +[`app.yaml`](./app.yaml) which you can also use to [deploy to your own AppEngine |
| 134 | +project](https://cloud.google.com/appengine/docs/flexible/php/download). |
110 | 135 |
|
111 | 136 | ## Contributing
|
112 | 137 |
|
113 |
| -We accept contributions via GitHub Pull Requests, but all contributors need to |
114 |
| -be covered by the standard Google Contributor License Agreement. You can find |
115 |
| -instructions for this in [CONTRIBUTING](CONTRIBUTING.md) |
| 138 | +No one ever has enough engineers, so we're very happy to accept contributions |
| 139 | +via Pull Requests. For details, see [CONTRIBUTING](CONTRIBUTING.md) |
0 commit comments