implement google captcha v2 in codeigniter 3
FX
1 min read
In your view file :
<div id="captchav2"></div>
Create a div
to display your captcha.
<input type="hidden" class="g-recaptcha-response" name="g-recaptcha-response">
Place it inside your form.
<script>
var onloadCallback = function() {
grecaptcha.render('captchav2', {
'sitekey' : 'YOUR SITE KEY'
});
};
</script>
<script src="https://www.google.com/recaptcha/api.js?onload=onloadCallback&render=explicit"
async defer>
</script>
Add towards the end of your view file.
In your controller function :
$captcha_response = trim($this->input->post('g-recaptcha-response'));
Add this inside your function.
if($captcha_response != ''): //true
{
$keySecret = 'YOUR SECRET KEY';
$check = array(
'secret' => $keySecret,
'response' => $this->input->post('g-recaptcha-response')
);
$startProcess = curl_init();
curl_setopt($startProcess, CURLOPT_URL, "https://www.google.com/recaptcha/api/siteverify");
curl_setopt($startProcess, CURLOPT_POST, true);
curl_setopt($startProcess, CURLOPT_POSTFIELDS, http_build_query($check));
curl_setopt($startProcess, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($startProcess, CURLOPT_RETURNTRANSFER, true);
$receiveData = curl_exec($startProcess);
$finalResponse = json_decode($receiveData, true);
if($finalResponse['success'])
{
//if success
}
else
{
//if failed
}
}
else
{
//if the user does not click the captcha v2
}
0
Subscribe to my newsletter
Read articles from FX directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by