How to add Google ReCAPTCHA in PHP Codeigniter

Date: 09-05-2020


reCAPTCHA is a free service from Google that helps protect websites from spam and abuse. It is verifying a user is a 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.

If you are creating a login form for your project, then it can change to hack by someone. So at that moment if you use google captcha then it provides the best security for prevent.

Step – 1:

We need to create reCAPTCHA for the required domain and generate a key pair like the site and secret key. We can use the following google link to create reCAPTCHA.

Click on this link then we will get the following page. Click on the Admin console.

Step – 2:

Once login completed by using Gmail login credentials,

Then we will have to register the site on which you are going to implement reCAPTCHA. We will get the following page.
Note: If you already register some domain name in the admin console then the Click Plus symbol on the Admin Console page opened for the setup form.

Step – 3:

We have to fill the following details in above page.

  • Label– Enter your domain name for which need to implement reCAPTCHA
  • reCAPTCHA type– Choose your preferred reCAPTCHA type.
  • Domains– Give the domain name to register.
  • Owners- Your Gmail id will be displayed by default.
  • Accept the reCAPTCHA Terms of Service– Click on the checkbox
  • Send alerts to owners– Click on the checkbox.

Then, click on the Submit button.

Step – 4:

After submitting, your website will be registered and click on the settings option then we will get the site and secret key by click on COPY SITE KEY & COPY SECRET KEY from the following page. Then click on the Save button.

Step – 5: To add site key on the PHP file.

Connect to your server via SSH as the root user using the following command:
ssh root@your server IP -p port number

We can open login.php by using the following command:
vi /home/domain name/application/views/login_view.php

We will add the following code in the required field on the login.php file.

<div class=”col-md-6″>
<div class=”g-recaptcha” data-sitekey=“enter site key”></div>
<div style=”margin-top: 5px;color: red; “><?php echo
form_error(‘g-recaptcha-response’); ?></div>

Note: enter site key – copy and paste the key from google reCAPTCHA.

Add the following code in the beginning or end on the login.php file.

<script src=’’></script>

Step – 6: To add secret key on the PHP file.

We need to start validating in the controller after form validation.

We can open login.php by using the following command:
vi /home/domain name/application/controllers/login.php

Now paste the code just after form validation of the following code.

$secret = ‘Enter secret key’; // CHANGE THIS TO YOUR OWN!
$url = “
$verify = json_decode(get_web_page($url));

Note: Enter secret key – copy and paste the key from google reCAPTCHA.

Google reCAPTCHA is now ready to use. You can use reCAPTCHA on the website login page.

Thank you!

Leave a Reply