Skip to content

Commit 29feb2a

Browse files
authored
Merge pull request google#110 from ismailbaskin/master
Add hostname property to Response
2 parents 8be9ec8 + 44df068 commit 29feb2a

File tree

3 files changed

+23
-2
lines changed

3 files changed

+23
-2
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,8 @@ $recaptcha = new \ReCaptcha\ReCaptcha($secret);
8383
$resp = $recaptcha->verify($gRecaptchaResponse, $remoteIp);
8484
if ($resp->isSuccess()) {
8585
// verified!
86+
// if Domain Name Validation turned off don't forget to check hostname field
87+
// if($resp->getHostName() === $_SERVER['SERVER_NAME']) { }
8688
} else {
8789
$errors = $resp->getErrorCodes();
8890
}

src/ReCaptcha/Response.php

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,12 @@ class Response
4343
*/
4444
private $errorCodes = array();
4545

46+
/**
47+
* the hostname of the site where the reCAPTCHA was solved
48+
* @var string
49+
*/
50+
private $hostName = '';
51+
4652
/**
4753
* Build the response from the expected JSON returned by the service.
4854
*
@@ -58,7 +64,7 @@ public static function fromJson($json)
5864
}
5965

6066
if (isset($responseData['success']) && $responseData['success'] == true) {
61-
return new Response(true);
67+
return new Response(true, array(), isset($responseData['hostname']) ? $responseData['hostname'] : '');
6268
}
6369

6470
if (isset($responseData['error-codes']) && is_array($responseData['error-codes'])) {
@@ -73,11 +79,13 @@ public static function fromJson($json)
7379
*
7480
* @param boolean $success
7581
* @param array $errorCodes
82+
* @param string $hostname
7683
*/
77-
public function __construct($success, array $errorCodes = array())
84+
public function __construct($success, array $errorCodes = array(), $hostname = '')
7885
{
7986
$this->success = $success;
8087
$this->errorCodes = $errorCodes;
88+
$this->hostName = $hostname;
8189
}
8290

8391
/**
@@ -99,4 +107,12 @@ public function getErrorCodes()
99107
{
100108
return $this->errorCodes;
101109
}
110+
111+
/**
112+
* @return string
113+
*/
114+
public function getHostName()
115+
{
116+
return $this->hostName;
117+
}
102118
}

tests/ReCaptcha/ResponseTest.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,9 @@ public function testIsSuccess()
5757

5858
$response = new Response(false);
5959
$this->assertFalse($response->isSuccess());
60+
61+
$response = new Response(true, array(), 'example.com');
62+
$this->assertEquals('example.com', $response->getHostName());
6063
}
6164

6265
public function testGetErrorCodes()

0 commit comments

Comments
 (0)