Skip to content

Commit 1d93893

Browse files
committed
Document additional validation
1 parent e825699 commit 1d93893

File tree

2 files changed

+47
-8
lines changed

2 files changed

+47
-8
lines changed

ARCHITECTURE.md

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
# Architecture
22

3-
The general pattern of usage is to instantiate the
4-
[`ReCaptcha`](./src/ReCaptcha/ReCaptcha.php) client with your secret key and
5-
then call `verify()` using the response from the JavaScript reCAPTCHA client.
3+
The general pattern of usage is to instantiate the `ReCaptcha` class with your
4+
secret key, specify any additional validation rules, and then call
5+
`verifyAndValidate()` with the reCAPTCHA response and user's IP address. For
6+
example:
67

78
```php
89
<?php
910
$recaptcha = new \ReCaptcha\ReCaptcha($secret);
10-
$resp = $recaptcha->verify($gRecaptchaResponse, $remoteIp);
11+
$resp = $recaptcha->setExpectedHostname('recaptcha-demo.appspot.com')
12+
->verifyAndValidate($gRecaptchaResponse, $remoteIp);
1113
if ($resp->isSuccess()) {
1214
// Verified!
1315
} else {

README.md

Lines changed: 41 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -63,21 +63,58 @@ functionality into your frontend.
6363

6464
This library comes in when you need to verify the user's response. On the PHP
6565
side you need the response from the reCAPTCHA service and secret key from your
66-
credentials. Instantiate the `ReCaptcha` class with your secret key and then
67-
pass the response to the `verify()` method.
66+
credentials. Instantiate the `ReCaptcha` class with your secret key, specify any
67+
additional validation rules, and then call `verifyAndValidate()` with the
68+
reCAPTCHA response and user's IP address. For example:
6869

6970
```php
7071
<?php
7172
$recaptcha = new \ReCaptcha\ReCaptcha($secret);
72-
$resp = $recaptcha->verify($gRecaptchaResponse, $remoteIp);
73+
$resp = $recaptcha->setExpectedHostname('recaptcha-demo.appspot.com')
74+
->verifyAndValidate($gRecaptchaResponse, $remoteIp);
7375
if ($resp->isSuccess()) {
7476
// Verified!
7577
} else {
7678
$errors = $resp->getErrorCodes();
7779
}
7880
```
7981

80-
For more usage details, see [ARCHITECTURE](ARCHITECTURE.md).
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 them
97+
together. For example:
98+
99+
```php
100+
<?php
101+
$recaptcha = new \ReCaptcha\ReCaptcha($secret);
102+
$resp = $recaptcha->setExpectedHostname('recaptcha-demo.appspot.com')
103+
->setExpectedAction('homepage')
104+
->setScoreThreshold(0.5)
105+
->verifyAndValidate($gRecaptchaResponse, $remoteIp);
106+
107+
if ($resp->isSuccess()) {
108+
// Verified!
109+
} else {
110+
$errors = $resp->getErrorCodes();
111+
}
112+
```
113+
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).
81118

82119
### Examples
83120

0 commit comments

Comments
 (0)