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.
Add this code into your webpage footer and then go to the back-end script view page and the snippet into your view page.
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);
$this->session->set_userdata(array('message'=>'reCAPTCHA input is not valid.')); redirect($_SERVER['HTTP_REFERER'], 'refresh');