How to Integrate google reCAPTCHA in your website
Google reCaptcha
is let you integrate captcha in your website’s form to protect your site from spammers and automatic abuses. Its quite user friendly as it doesn’t verify human by using traditional method of verifying text displayed in image. User just need to select correct images from grid which is answer of question. If user has already logged in google account then there is no need of verification process. Here is step by step tutorial to add recaptcha in your page.
First of all you need to create application and register our website in google console.
Click on google reCaptcha and click on “Get reCaptcha”. Now, login with google account. Add application name and add site(s) you want to use reCaptcha and . It also allows multiple domains. After entering all details, Now, note site key and Google secret key. It will need later in implementation.
index.html
It contains simple HTML form wtih google reCaptcha snippet. Here, we have create contact us form.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
<html>
<head>
<script src="https://www.google.com/recaptcha/api.js"></script>
</head>
<body>
<p class='error'><?php echo $error; ?></p>
<form action="contactus.php" method="post">
<div>Your Name</div>
<input type="text" name="firstName" class="input" />
<div>Message</div>
<textarea name="message"></textarea>
<div class="g-recaptcha" data-sitekey="Google Site Key"></div>
<input type="submit" value="Send" />
</form>
</body>
</html>
|
In above code, Replace “Google Site Key” with your site key.
contactus.php
Will check captcha you have entered is valid or not. If captcha is valid then it will send email to perticular email address. Replace “To email address” with your email address. Here you need to Enable php_curl
extensation in php.ini
file.How to enable curl in php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
|
<?php
$response = "";
if($_POST['REQUEST_METHOD'] == "POST"){
if(isset($_POST['message']) && isset($_POST['firstName'])){
$message = $_POST['message'];
$firstName = $_POST['firstName'];
$captcha = $_POST['g-recaptcha-response'];
if(!empty($captcha)){
if(!empty($captcha)){
$google_url="https://www.google.com/recaptcha/api/siteverify";
$key = 'Google Secret Key';
$ip = $_SERVER['REMOTE_ADDR'];
$url = $google_url."?secret=".$key ."&response=".$recaptcha."&remoteip=".$ip;
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_TIMEOUT, 10);
$curlData = curl_exec($curl);
curl_close($curl);
$curlData = json_decode($curlData, true);
if($curlData['success']){
mail("To email address", "Contact us message from ".$firstName, $message);
} else {
$response = "Invalid reCAPTCHA";
}
} else {
$response = "Please re-enter reCAPTCHA code";
}
} else {
$response = "Please enter required details";
}
}
}
echo $response;
?>
|
clay smith
I have just found your blog, and I’m in love with your writing skills. I decided to comment to this post because it really helped me out. Keep up the good work