Skip to content

Commit

Permalink
Update to ReCAPTCHA v3.
Browse files Browse the repository at this point in the history
  • Loading branch information
JoelLisenby committed Apr 3, 2019
1 parent 59d3cba commit ff87a20
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 6 deletions.
22 changes: 20 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
# recaptcha
A super simple PHP class for use with [reCAPTCHA](https://www.google.com/recaptcha/admin)'s new Invisible ReCAPTCHA API.
A super simple PHP class for use with [reCAPTCHA](https://www.google.com/recaptcha/admin)'s new ReCAPTCHA v3 with scores.

# usage
(See [index.php](index.php) for a working example)

Before you begin, you'll need a site key and secret key from https://www.google.com/recaptcha/ (choose invisible reCAPTCHA option)
Before you begin, you'll need a site key and secret key from https://www.google.com/recaptcha/ (choose reCAPTCHA v3 option)

**1** Include recaptcha.class.php and initiate with your site key and secret key
```php
Expand All @@ -26,3 +26,21 @@ echo $recaptcha->button('Button Value');
```php
if($recaptcha->success()) { ... }
```
**5** Optional: Use the get_response() function to gain access to the score so you can make your own definition of "success"
```php
$response = $recaptcha->get_response();
$score = $response->score; // Score between 0.0 and 1.0. 1.0 is very likely a good interaction, 0.0 is very likely a bot
$success = $response->success; // true is a valid reCAPTCHA token for your site
```

Complete get_response() returns:
```
{
"success": true|false, // whether this request was a valid reCAPTCHA token for your site
"score": number // the score for this request (0.0 - 1.0)
"action": string // the action name for this request (important to verify)
"challenge_ts": timestamp, // timestamp of the challenge load (ISO format yyyy-MM-dd'T'HH:mm:ssZZ)
"hostname": string, // the hostname of the site where the reCAPTCHA was solved
"error-codes": [...] // optional
}
```
7 changes: 6 additions & 1 deletion index.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,12 @@
$form = ob_get_clean();

if(!empty($_POST)) {
if($recaptcha->success()) {
$response = $recaptcha->get_response();

echo "Success: ". ( $response->success ? 'true' : 'false' ) ."<br />\r\n";
echo "Score: ". $response->score ."<br />\r\n";

if($response->success) {
echo "Passed Captcha.";
} else {
echo "Failed Captcha.";
Expand Down
10 changes: 7 additions & 3 deletions recaptcha.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ public function __construct($form_id, $sitekey, $secret) {
$this->sitekey = $sitekey;
$this->secret = $secret;
}
public function success() {

public function get_response() {
if(!empty($_POST['g-recaptcha-response'])) {
$url = 'https://www.google.com/recaptcha/api/siteverify';
$data = array(
Expand All @@ -30,12 +30,16 @@ public function success() {
$context = stream_context_create($options);
$result = json_decode(file_get_contents($url, false, $context));

return $result->success;
return $result;
} else {
return false;
}
}

public function success() {
return $this->get_response()->success;
}

public function script() {
ob_start();
?>
Expand Down

0 comments on commit ff87a20

Please sign in to comment.