integrate google recaptcha with codeigniter validation
In this post we will show you integrate google recaptcha with codeigniter validation, hear for integrate google recaptcha with codeigniter validation we will give you demo and example for implement.
we didnt have whenever to check cpanel,just go to dashboard,add new instructional exercise and its done,one day we take alook to cpanel and we amazed with many spam comments,so we thought to add recaptcha to remarks form,but on the grounds that we am so bustling we thought to take a simple approach to do that.and as we stated,
There is dependably a simple approach to learn stuff, all you need to discover.
in this instructional exercise you will take in the esy approach to coordinate recaptcha 2 benefit programming interface with codeigniter without need to expand any library or compose more code,this undertaking will influence our site more to secure by add it to codeigniter approval rules, lets do it.
Code for integrate google recaptcha with codeigniter validation
1 step # get an api key for your site from google,
integrate_recaptcha_with_codeigniter_validation
you must have google account ,go to this link and fill form like image
https://www.google.com/recaptcha/admin
2 Step # copy generated api key like this image
integrate_recaptcha_with_codeigniter_validation
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="ADD_YOUR_GOOGLE_SITE_KEY_HERE"></div>
3 Step # into your controller put this validation function
function google_validate_captcha() { $google_captcha = $this->input->post('g-recaptcha-response'); $google_response = file_get_contents("https://www.google.com/recaptcha/api/siteverify?secret=your secret key here &response=" . $google_captcha . "&remoteip=" . $_SERVER['REMOTE_ADDR']); if ($google_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 google_validate_captcha() { $google_recaptcha = trim($this->input->post('g-recaptcha-response')); $google_userIp= $this->input->ip_address(); // add your secret key hear $google_secret='Add-your-secret-key-hear'; $captcha_data = array( 'secret' => "$google_secret", 'response' => "$google_recaptcha", 'remoteip' =>"$google_userIp" ); $captcha_verify = curl_init(); curl_setopt($captcha_verify, CURLOPT_URL, "https://www.google.com/recaptcha/api/siteverify"); curl_setopt($captcha_verify, CURLOPT_POST, true); curl_setopt($captcha_verify, CURLOPT_POSTFIELDS, http_build_query($captcha_data)); curl_setopt($captcha_verify, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($captcha_verify, CURLOPT_RETURNTRANSFER, true); $captcha_response = curl_exec($captcha_verify); $captcha_status= json_decode($captcha_response, true); if(empty($captcha_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('google_validate_captcha', 'Please check the the captcha form');
Hope this code and post will helped you for implement integrate google recaptcha with codeigniter validation. if you need any help or any feedback give it in comment section or you have good idea about this post you can give it comment section. Your comment will help us for help you more and improve onlincode. we will give you this type of more interesting post in featured also so, For more interesting post and code Keep reading our blogs onlincode.org