How to add Google reCAPTCHA in CodeIgniter

Hi guys today, After a long time I am back with a new tutorial. Today I am going to show you about Google reCAPTCHA using in CodeIgniter. If you want to use Google reCAPTCHA in CodeIgniter then this tutorial is perfect to help you. I hope you know what is ReCaptcha? If you don't know that we are starting from the first.

What is Google reCAPTCHA?

Google reCAPTCHA is a service offered by Google which verifies a user human or robot. Google reCAPTCHA show some images and give hint in text and ask users to select appropriate images on the subject. If users select perfectly then google verified that the user is not a robot.

Why should use  Google reCAPTCHA?

You should use  Google reCAPTCHA to prevent robots access to your sensitive pages like login page, registration page, reset form page etc. If you use  Google reCAPTCHA on your website robots could not be registered and couldn't be trying for login.  Google reCAPTCHA also help you to prevent Brute-force attack on your website. Now we are going to the main part of this tutorial.

How to use  Google reCAPTCHA in CodeIgniter?

For using the Google reCAPTCHA you have to go Google reCAPCTHA website and click in My reCAPTCHA from the right top from the website. Then type label click select a version between reCAPTCHA v2 and reCAPCTHA v3 and then type domains which you want and click accept terms and then finally click Register button. 

Now you will be entered in the main page of Google reCAPTCHA page which you can find the necessary items which help you to use Google reCAPTCHA. You will find Site key and Secret key on the page.

Now click on  Step 1: Client side integration to integrate reCAPTCHA with your website. 

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

Add this code into your webpage footer and then go to the back-end script view page and the snippet into your view page.

<div class="g-recaptcha" data-sitekey="6LceHSQTAAAAAB3KdV7X215KMq1jJnVaxF0rWfpO"></div>

Change site key as your key which is provided by google next go to your controller and start validating in the controller after form validation. Now paste the code just after form validation of this code-

    $secret = 'YOUR-GOOGLE-RECAPCTHA SECRATE';
    $response = $this->input->post('g-recaptcha-response');
        $verifyResponse = file_get_contents('https://www.google.com/recaptcha/api/siteverify?secret='.$secret.'&response='.$response);
        $responseData = json_decode($verifyResponse);
        if(!$responseData->success){

    $this->session->set_userdata(array('message'=>'reCAPTCHA input is not valid.'));

    redirect($_SERVER['HTTP_REFERER'], 'refresh');

    } 

replace secret key from Google reCAPTCHA page and then start using. Your Google reCAPTCHA is now ready to use.

This is an easy way to use Google reCAPTCHA and start using today. If you like this tutorial please share this with your friends.

We use cookies on our website. To find out more about how and why they are used or opt-out, please read our Cookie Policy. By choosing "I Accept", you consent to our use of cookies. Cookie Policy