07-26-2023, 04:35 PM
I'm trying to add google reCaptcha to prestashop's opc registration page, I have the checkbox setup and it's working however the php file is not receiving the captcha response. Let me explain. Here's what I've done so far: In my header.tpl I added
<script src='https://www.google.com/recaptcha/api.js'></script>
In order-opc-new-account.tpl, I added
<div class="g-recaptcha" data-sitekey="[my_public_key]"></div>
In my AuthController.php I added
if (Tools::isSubmit('submitAccount') OR Tools::isSubmit('submitGuestAccount')) //if statement was already present
{
// captcha code I added
$reCaptchaUrl='https://www.google.com/recaptcha/api/siteverify';
$reCaptchaSecret = '[my_secret_key]';
$reCaptchaResponse = $_POST['g-recaptcha-response'];
$ip = $_SERVER['REMOTE_ADDR'];
$verifyCaptcha = file_get_contents($reCaptchaUrl."?secret=".$reCaptchaSecret."&response=".$reCaptchaResponse."&remoteip=".$ip);
$captchaReply = json_decode($verifyCaptcha);
if(isset($captchaReply->success) AND $captchaReply->success == true){
$logger = new FileLogger(0);
$logger->setFilename(_PS_ROOT_DIR_."/log/debug.log");
$logger->logDebug("Captcha was successful: ".$reCaptchaResponse);
} else {
//captcha failed
$logger = new FileLogger(0);
$logger->setFilename(_PS_ROOT_DIR_."/log/debug.log");
$logger->logDebug("Captcha failed: ".$reCaptchaResponse);
}
// ... prestashop registration code
}
I learned with the debugging messages that `$reCaptchaResponse` variable is actually coming empty everytime, even if the captcha has been checked. Any ideas why?
**Edit**: The form actually sends the data to `authentication.php` which has the following lines
require(dirname(__FILE__).'/config/config.inc.php');
ControllerFactory::getController('AuthController')->run();
My guess is that this piece of code *forwards* the form data to `AuthController.php` but it only forwards fields that it's been told to forward. It does not recognizes the new recaptcha field and doesn't forward that data to the file. So I would need to find who decides which data get's forwarded.
<script src='https://www.google.com/recaptcha/api.js'></script>
In order-opc-new-account.tpl, I added
<div class="g-recaptcha" data-sitekey="[my_public_key]"></div>
In my AuthController.php I added
if (Tools::isSubmit('submitAccount') OR Tools::isSubmit('submitGuestAccount')) //if statement was already present
{
// captcha code I added
$reCaptchaUrl='https://www.google.com/recaptcha/api/siteverify';
$reCaptchaSecret = '[my_secret_key]';
$reCaptchaResponse = $_POST['g-recaptcha-response'];
$ip = $_SERVER['REMOTE_ADDR'];
$verifyCaptcha = file_get_contents($reCaptchaUrl."?secret=".$reCaptchaSecret."&response=".$reCaptchaResponse."&remoteip=".$ip);
$captchaReply = json_decode($verifyCaptcha);
if(isset($captchaReply->success) AND $captchaReply->success == true){
$logger = new FileLogger(0);
$logger->setFilename(_PS_ROOT_DIR_."/log/debug.log");
$logger->logDebug("Captcha was successful: ".$reCaptchaResponse);
} else {
//captcha failed
$logger = new FileLogger(0);
$logger->setFilename(_PS_ROOT_DIR_."/log/debug.log");
$logger->logDebug("Captcha failed: ".$reCaptchaResponse);
}
// ... prestashop registration code
}
I learned with the debugging messages that `$reCaptchaResponse` variable is actually coming empty everytime, even if the captcha has been checked. Any ideas why?
**Edit**: The form actually sends the data to `authentication.php` which has the following lines
require(dirname(__FILE__).'/config/config.inc.php');
ControllerFactory::getController('AuthController')->run();
My guess is that this piece of code *forwards* the form data to `AuthController.php` but it only forwards fields that it's been told to forward. It does not recognizes the new recaptcha field and doesn't forward that data to the file. So I would need to find who decides which data get's forwarded.