Skip to content

Commit 68dd1ef

Browse files
authored
Merge pull request #589 from google/eliminate-magic-constants-usage
Eliminate magic constants usage
2 parents 7ef9881 + d6871ad commit 68dd1ef

6 files changed

Lines changed: 31 additions & 21 deletions

File tree

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ This library comes in when you need to verify the user's response. On the PHP
7070
side you need the response from the reCAPTCHA service and secret key from your
7171
credentials. Instantiate the `ReCaptcha` class with your secret key, specify any
7272
additional validation rules, and then call `verify()` with the reCAPTCHA
73-
response (usually in `$_POST['g-recaptcha-response']` or the response from
73+
response (usually in `$_POST[\ReCaptcha\ReCaptcha::USER_TOKEN_PARAMETER]` or the response from
7474
`grecaptcha.execute()` in JS which is in `$gRecaptchaResponse` in the example)
7575
and user's IP address. For example:
7676

examples/config.php.dist

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
<?php
2+
23
/**
3-
* BSD 3-Clause License
4+
* BSD 3-Clause License.
5+
*
46
* @copyright (c) 2019, Google Inc.
5-
* @link https://www.google.com/recaptcha
7+
*
8+
* @see https://www.google.com/recaptcha
69
* All rights reserved.
710
*
811
* Redistribution and use in source and binary forms, with or without
@@ -31,16 +34,16 @@
3134
*/
3235

3336
return [
34-
'v2-standard' => [
35-
'site' => '',
36-
'secret' => '',
37-
],
38-
'v2-invisible' => [
39-
'site' => '',
40-
'secret' => '',
41-
],
42-
'v3' => [
43-
'site' => '',
44-
'secret' => '',
45-
],
37+
'v2-standard' => [
38+
'site' => '',
39+
'secret' => '',
40+
],
41+
'v2-invisible' => [
42+
'site' => '',
43+
'secret' => '',
44+
],
45+
'v3' => [
46+
'site' => '',
47+
'secret' => '',
48+
],
4649
];

examples/recaptcha-v2-checkbox-explicit.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@
8282
<h2>Add your keys</h2>
8383
<p>If you do not have keys already then visit <kbd> <a href = "https://www.google.com/recaptcha/admin">https://www.google.com/recaptcha/admin</a></kbd> to generate them. Edit this file and set the respective keys in the <kbd>config.php</kbd> file or directly to <kbd>$siteKey</kbd> and <kbd>$secret</kbd>. Reload the page after this.</p>
8484
<?php
85-
} elseif (isset($_POST['g-recaptcha-response'])) {
85+
} elseif (isset($_POST[ReCaptcha::RESPONSE_KEY])) {
8686
// The POST data here is unfiltered because this is an example.
8787
// In production, *always* sanitise and validate your input'
8888
?>
@@ -99,7 +99,7 @@
9999
// $recaptcha = new \ReCaptcha\ReCaptcha($secret, new \ReCaptcha\RequestMethod\SocketPost());
100100
// Make the call to verify the response and also pass the user's IP address
101101
$resp = $recaptcha->setExpectedHostname($_SERVER['SERVER_NAME'])
102-
->verify($_POST['g-recaptcha-response'], $_SERVER['REMOTE_ADDR'])
102+
->verify($_POST[ReCaptcha::RESPONSE_KEY], $_SERVER['REMOTE_ADDR'])
103103
;
104104

105105
if ($resp->isSuccess()) {

examples/recaptcha-v2-checkbox.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@
8282
<h2>Add your keys</h2>
8383
<p>If you do not have keys already then visit <kbd> <a href = "https://www.google.com/recaptcha/admin">https://www.google.com/recaptcha/admin</a></kbd> to generate them. Edit this file and set the respective keys in the <kbd>config.php</kbd> file or directly to <kbd>$siteKey</kbd> and <kbd>$secret</kbd>. Reload the page after this.</p>
8484
<?php
85-
} elseif (!empty($_POST['g-recaptcha-response'])) {
85+
} elseif (!empty($_POST[ReCaptcha::RESPONSE_KEY])) {
8686
// The POST data here is unfiltered because this is an example.
8787
// In production, *always* sanitise and validate your input'
8888
?>
@@ -100,7 +100,7 @@
100100

101101
// Make the call to verify the response and also pass the user's IP address
102102
$resp = $recaptcha->setExpectedHostname($_SERVER['SERVER_NAME'])
103-
->verify($_POST['g-recaptcha-response'], $_SERVER['REMOTE_ADDR'])
103+
->verify($_POST[ReCaptcha::RESPONSE_KEY], $_SERVER['REMOTE_ADDR'])
104104
;
105105
if ($resp->isSuccess()) {
106106
// If the response is a success, that's it!

examples/recaptcha-v2-invisible.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@
8282
<h2>Add your keys</h2>
8383
<p>If you do not have keys already then visit <kbd> <a href = "https://www.google.com/recaptcha/admin">https://www.google.com/recaptcha/admin</a></kbd> to generate them. Edit this file and set the respective keys in <kbd>$siteKey</kbd> and <kbd>$secret</kbd>. Reload the page after this.</p>
8484
<?php
85-
} elseif (isset($_POST['g-recaptcha-response'])) {
85+
} elseif (isset($_POST[ReCaptcha::RESPONSE_KEY])) {
8686
// The POST data here is unfiltered because this is an example.
8787
// In production, *always* sanitise and validate your input'
8888
?>
@@ -100,7 +100,7 @@
100100

101101
// Make the call to verify the response and also pass the user's IP address
102102
$resp = $recaptcha->setExpectedHostname($_SERVER['SERVER_NAME'])
103-
->verify($_POST['g-recaptcha-response'], $_SERVER['REMOTE_ADDR'])
103+
->verify($_POST[ReCaptcha::RESPONSE_KEY], $_SERVER['REMOTE_ADDR'])
104104
;
105105
if ($resp->isSuccess()) {
106106
// If the response is a success, that's it!

src/ReCaptcha/ReCaptcha.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,13 @@ class ReCaptcha
5656
*/
5757
public const SITE_VERIFY_URL = 'https://www.google.com/recaptcha/api/siteverify';
5858

59+
/**
60+
* User response token parameter name.
61+
*
62+
* @var string
63+
*/
64+
public const RESPONSE_KEY = 'g-recaptcha-response';
65+
5966
/**
6067
* Invalid JSON received.
6168
*

0 commit comments

Comments
 (0)