Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Google Recaptcha v3 gives invalid response #527

Open
DoctorDred opened this issue Jul 14, 2023 · 0 comments
Open

Google Recaptcha v3 gives invalid response #527

DoctorDred opened this issue Jul 14, 2023 · 0 comments

Comments

@DoctorDred
Copy link

Issue description
Google Recaptcha v3 gives invalid response.
Installed by compressor command - composer require google/recaptcha "^1.2"

Please to solve the problem.

In controler file plase code:
`require DIR . '/appengine-https.php';

//reCAPTCHA PHP client library
require_once '/root/www/site-root/vendor/google/recaptcha/src/autoload.php';

//Secret key
$siteKey = 'My key';
$secret = 'My key';

// Copy the config.php.dist file to config.php and update it with your keys
$config = include DIR . '/config.php';
if ($siteKey == '' && is_readable(DIR . '/root/www/site-root/wwww/config.php')) {
$siteKey = $config['v3']['site'];
$secret = $config['v3']['secret'];
}
// reCAPTCHA supports 40+ languages listed here: https://developers.google.com/recaptcha/docs/language
$lang = 'en';
$lang = 'ru';

$recaptcha = new \ReCaptcha\ReCaptcha($secret);

$resp = $recaptcha
->setExpectedHostname('my_site.com')
->setExpectedAction('homepage')
->setScoreThreshold(0.5)
->verify('token', $_SERVER['REMOTE_ADDR']);

if (!$resp->isSuccess()) {
//$this->error_msg = 'ReCaptcha field is required.';

}

if ($resp->getScore() < 0.5) {
header('Location: success.htm');

exit();
//$recaptchaErrors = $response->getErrorCodes();

} else {
// otherwise, let the spammer think that they got their message through
//header('Location: success.htm');
//exit();

}`

In .tpl file plased code:

`




<title>Google reCAPTCHA</title>

<script src='https://www.google.com/recaptcha/api.js?render=site_key' ></script>

<div class="row"><?php echo $column_left; ?><?php echo $column_right; ?>
<?php $column_left  = trim($column_left);
$column_right  = trim($column_right); ?>
<?php if ($column_left && $column_right) { ?>
<?php $class = 'col-sm-12'; ?>
<?php } elseif ($column_left || $column_right) { ?>
<?php $class = 'col-sm-18'; ?>
<?php } else { ?>
<?php $class = 'col-sm-24'; ?>
<?php } ?>
<div id="content" class="<?php echo $class; ?>"><?php echo $content_top; ?>


<?php if ($error_name) { ?>
<span class="error"><?php echo $error_name; ?></span>
<?php } ?>

<span><?php echo $entry_email; ?></span><br />
<input type="text" class="form-control" name="email" value="<?php echo $email; ?>" />

<?php if ($error_email) { ?>
<span class="error"><?php echo $error_email; ?></span>
<?php } ?>

<span><?php echo $entry_enquiry; ?></span><br />
<textarea class="form-control" name="enquiry" cols="40" rows="10"><?php echo $enquiry; ?></textarea>

<?php if ($error_enquiry) { ?>
<span class="error"><?php echo $error_enquiry; ?></span>


<?php } ?>
</div>
<br/>
  <div class="left"><input id="token" type="hidden" name="token"></div>
   <div class="buttons">
  <div class="left"><input type="submit" name="btnSubmit" id="submit" value="<?php echo $button_send; ?>" class="btn" /></div>
</div>
</form>
<script> grecaptcha.ready(function() { grecaptcha.execute('site_key', {action: 'homepage'}).then(function(token) { console.log(token); document.getElementById('token').value = token }); });</script>
      </body>
    <?php echo $content_bottom; ?>
  <style>
  input[type="submit"]{
  margin-bottom: 50px;
  margin-left: 365px;
  padding: 7px 25px;
  }
  </style>
</div></div></div>

`

print_r($recaptcha); ---->

ReCaptcha\ReCaptcha Object
(
[secret:ReCaptcha\ReCaptcha:private] => bhfbj4bfi48y8fhu9023ijr4fnhu5fbnjf5eofj5nf5inf5in5
[requestMethod:ReCaptcha\ReCaptcha:private] => ReCaptcha\RequestMethod\Post Object
(
[siteVerifyUrl:ReCaptcha\RequestMethod\Post:private] => https://www.google.com/recaptcha/api/siteverify
)

[hostname] => my_syte.com
[action] => homepage
[threshold] => 0.5

print_r($resp); ----->

ReCaptcha\Response Object
(
[success:ReCaptcha\Response:private] =>
[errorCodes:ReCaptcha\Response:private] => Array
(
[0] => invalid-input-response
[1] => hostname-mismatch
[2] => action-mismatch
[3] => score-threshold-not-met
)

[hostname:ReCaptcha\Response:private] => 
[challengeTs:ReCaptcha\Response:private] => 
[apkPackageName:ReCaptcha\Response:private] => 
[score:ReCaptcha\Response:private] => 
[action:ReCaptcha\Response:private] =>

Environment
LinuxLinux

  • OS name and version: Linux
  • PHP version: 7.4
  • Web server name and version: Opencart
  • google/recaptcha version: 1.2
  • Browser name and version: Chrome last updates

Reproducing the issue
On demand please.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant