Tutorials

Article Content:integrate recaptcha with codeigniter validation

integrate recaptcha with codeigniter validation

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

      https://www.google.com/recaptcha/admin

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 :)



  • Colo
    Locally 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: 180
    April 19, 2016
    Reply
    • admin
      info@webeasystep.com
      colo , try to add success between quotes and it will work,if not tell me please thanks
      April 19, 2016
      Reply
    • admin
      info@webeasystep.com
      sure because you need to change the domain in the key settings because you moved from localhost to live domain
      January 7, 2019
      Reply
  • Colo
    Actually 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. Thanks
    April 19, 2016
    Reply
    • admin
      info@webeasystep.com
      you are welcome colo :)
      April 20, 2016
      Reply
  • gagas
    thanks bro !
    September 23, 2017
    Reply
  • abubakkar
    thank 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 again
    January 5, 2019
    Reply
  • Eder
    Hi! How to integrate into Codeigniter 4?
    August 16, 2019
    Reply
    • admin
      info@webeasystep.com
      Hi Eder , i will update this tutorial soon for CI 4 integration
      August 18, 2019
      Reply

Leave a Reply

Your email address will not be published.


Notify me of followup comments via e-mail.
You can also Subscribe without commenting.