integrate recaptcha with codeigniter validation.
about three weeks later,i didnt have any time to check cpanel,just go to dashboard,add new tutorial and its done,one day i take alook to cpanel and i surprised with many spam comments,so i thought to add recaptcha to comments form,but because i am so busy i thought to take an easy way to do that.and as we said,
There is always an easy way to learn stuff, all you have to find out
after this i thought to teach you dude :)
in this tutorial you will learn the esy way to integrate recaptcha 2 service api with codeigniter without need to extend any library or write more code,this task will make our site more secure by add it to codeigniter validation rules, lets do it
1-First step : get an api key for your site from google, you must have google account ,go to this link and fill form like image
2 - Step two : copy generated api key like this image
and put recaptcha input into your form then put your api code into it, then put api link above the same file
<script src='https://www.google.com/recaptcha/api.js'></script>
<div class="g-recaptcha" data-sitekey="Put_YOUR_GOOGLE_SITE_KEY_HERE"></div>
3- Step three : into your controller put this validation function
function validate_captcha() {
$captcha = $this->input->post('g-recaptcha-response');
$response = file_get_contents("https://www.google.com/recaptcha/api/siteverify?secret=your secret key here &response=" . $captcha . "&remoteip=" . $_SERVER['REMOTE_ADDR']);
if ($response . 'success' == false) {
return FALSE;
} else {
return TRUE;
}
}
or you can use curl if file_get_contents not supported by your hosting provider like this
function validate_captcha() {
$recaptcha = trim($this->input->post('g-recaptcha-response'));
$userIp= $this->input->ip_address();
$secret='gfdgdgdgdgdg-bgSCYqSo-Y-vA';
$data = array(
'secret' => "$secret",
'response' => "$recaptcha",
'remoteip' =>"$userIp"
);
$verify = curl_init();
curl_setopt($verify, CURLOPT_URL, "https://www.google.com/recaptcha/api/siteverify");
curl_setopt($verify, CURLOPT_POST, true);
curl_setopt($verify, CURLOPT_POSTFIELDS, http_build_query($data));
curl_setopt($verify, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($verify, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($verify);
$status= json_decode($response, true);
if(empty($status['success'])){
return FALSE;
}else{
return TRUE;
}
}
Then add this validation rule and validation message
$this->form_validation->set_rules('g-recaptcha-response', 'recaptcha validation', 'required|callback_validate_captcha');
$this->form_validation->set_message('validate_captcha', 'Please check the the captcha form');
And that is it,i hope it help you dude :)
-
ColoLocally using Xampp it works fine. When Live, it throws an error: A PHP Error was encountered Severity: Notice Message: Use of undefined constant success - assumed 'success' Filename: controllers/Contact.php Line Number: 180Reply
-
ColoActually I tried inserting success in the quotes like this 'success' straight after I sent you the email and it worked.. Sorry I forgot to inform you about that. ThanksReply
-
admininfo@webeasystep.comyou are welcome colo :)Reply
-
-
gagasthanks bro !Reply
-
abubakkarthank you so much it complete my work and you explanation and share information with us i appreciate what you share with us Thanks you so much once againReply
-
EderHi! How to integrate into Codeigniter 4?Reply
-
admininfo@webeasystep.comHi Eder , i will update this tutorial soon for CI 4 integrationReply
-