Reputation: 5
I moved my website and domain to a new hoster and since then Google Recaptcha has stopped working.
I haven't changed anything in the code and the domain has also remained the same.
This is my PHP code to check the verification it has worked for years
<?php
$response = $_POST["g-recaptcha-response"];
$url = 'https://www.google.com/recaptcha/api/siteverify';
$data = array(
'secret' => '************************',
'response' => $_POST["g-recaptcha-response"]
);
$options = array(
'http' => array (
'method' => 'POST',
'content' => http_build_query($data)
)
);
$context = stream_context_create($options);
$verify = file_get_contents($url, false, $context);
$captcha_success=json_decode($verify);
if ($captcha_success->success==false) {
echo '{ "alert": "error", "message": "<strong>Captcha Check Fail</strong> Bitte versuche es nocheinmal." }';
} else if ($captcha_success->success==true) {
}
?>
this is a finished script from the internet, but it doesn't work either. I've tried several finished scripts but none of them work
<script src="https://google.com/recaptcha/api.js" async defer></script>
<form action="" method="POST" style="width: 80%; margin-left: 497px;">
<div style="margin-left: 48px;"><b> Registration Form </b><br><br></div>
<div>Name: <input type="text" name="name" value="" /><br><br></div>
<div>Email: <input type="text" name="email" value="" /><br><br></div>
<div class="g-recaptcha" data-sitekey="******************"></div><br><br>
<input type="submit" name="submit" value="SUBMIT">
</form>
<?php
if(isset($_POST['submit']) && $_POST['submit'] == 'SUBMIT'){
if(isset($_POST['g-recaptcha-response']) && !empty($_POST['g-recaptcha-response']))
{
$secret = '*********************';
$verifyResponse = file_get_contents('https://google.com/recaptcha/api/siteverify?secret='.$secret.'&response='.$_POST['g-recaptcha-response']);
$responseData = json_decode($verifyResponse);
if($responseData->true)
{ ?>
<div style="color: limegreen;"><b>Your contact request have submitted successfully.</b></div>
<?php }
else
{?>
<div style="color: red;"><b>Robot verification failed, please try again.</b></div>
<?php }
}else{?>
<div style="color: red;"><b>Please do the robot verification.</b></div>
<?php }
}
?>
Of course I have deleted the Recaptcha account several times and generated new tokens, but nothing changes. can it also be due to the hoster? are there any PHP settings that prevent recapatcha? the server runs on PHP 7.4 and the domain has an SSL certificate from lets encrypt.
thank you for your help
Upvotes: 0
Views: 580
Reputation: 2519
Adding as an answer here for anyone else seeking similar advice.
The issue was that the new host didn't have allow_url_fopen
enabled in the PHP configuration.
This can be checked by searching the output of a phpinfo();
call.
Enabling allow_url_fopen
can usually be achieved by editing your php.ini file, making changes in php settings in cPanel or Plesk or by contacting your hosting provider's support team.
file_get_contents
requires allow_url_fopen
.
Upvotes: 0