php

Google Recaptcha in CodeIgniter

It is one of the major discussed question, how we can use google recaptcha in codeigniter.First we should need a google account to start. Login the corresponding gmail and access https://www.google.com/recaptcha/admin from your browser. Next you have to register the domain. After that you will get a site key and secret key.

Herecomes the our coding part, insert following part in header

<script src='https://www.google.com/recaptcha/api.js'></script>

For showing the recpatcha section, use the below html code. Complete the code by using your data site key.

<div class="g-recaptcha" data-sitekey="ENTER YOUR SITE KEY"></div>

Add the secret key in your controller with following code.

$recaptchaResponse = trim($this->input->post('g-recaptcha-response'));
            $secret='ENTER THE SECRET KEY';
       
            $credential = array(
                    'secret' => $secret,
                    'response' => $this->input->post('g-recaptcha-response')
                );
                $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($credential));
                curl_setopt($verify, CURLOPT_SSL_VERIFYPEER, false);
                curl_setopt($verify, CURLOPT_RETURNTRANSFER, true);
                $response = curl_exec($verify);
           
                $status= json_decode($response, true);
                if($status['success'])
            {
			 echo “sucesss”;
			}
                 Else{
			  Echo “fail”;
			}
Posts created 494

Related Posts

Begin typing your search term above and press enter to search. Press ESC to cancel.

Back To Top