Google has discharged the new reCAPTCHA. Utilizing reCAPTCHA clients can demonstrate they are human without understanding a CAPTCHA. They require only a solitary snap to affirm they are not a robot. In this way, reCAPTCHA will shield your site from spam with better client encounter. You can without much of a stretch incorporate Google reCAPTCHA in PHP script.
We have made a contact frame with the new Google reCAPTCHA utilizing PHP. Investigate the demo of Google reCAPTCHA in PHP from the Demo connect. The reCAPTCHA joining procedure is given underneath.
Get reCAPTCHA api keys:
For adding reCAPTCHA to your site, you have to enlist your site and get reCAPTCHA API keys.
Enlist your site at Google from here – https://www.google.com/recaptcha/admin
Get your site key that's used to display the reCAPTCHA widget.
Get your Secret key helps authorizes communication between your site and the reCAPTCHA server.
Html Code :-
<script src="https://www.google.com/recaptcha/api.js" async defer></script>
<form action="" method="POST">
<input type="text" name="name" value="" />
<input type="text" name="email" value="" />
<textarea type="text" name="message"></textarea>
<div class="g-recaptcha" data-sitekey="9ABCGUTKOHdJnGhsKH--DDHD"></div>
<input type="submit" name="submit" value="SUBMIT">
</form>
Php Code :-
<?php
if(isset($_POST['submit']) && !empty($_POST['submit'])):
if(isset($_POST['g-recaptcha-response']) && !empty($_POST['g-recaptcha-response'])):
//your site secret key
$secret = '6LeXrhAUAAAAAMnkNXY0S8AyUI0vklLR5jaSfK5L';
//get verify response data
$verifyResponse = file_get_contents('https://www.google.com/recaptcha/api/siteverify?secret='.$secret.'&response='.$_POST['g-recaptcha-response']);
$responseData = json_decode($verifyResponse);
if($responseData->success):
//contact form submission code
$name = !empty($_POST['name'])?$_POST['name']:'';
$email = !empty($_POST['email'])?$_POST['email']:'';
$message = !empty($_POST['message'])?$_POST['message']:'';
$to = 'yogi.lalit2391@gmail.com';
$subject = 'New contact form have been submitted';
$htmlContent = "
<h1>Contact request details</h1>
<p><b>Name: </b>".$name."</p>
<p><b>Email: </b>".$email."</p>
<p><b>Message: </b>".$message."</p>
";
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";
$headers .= 'From:'.$name.' <'.$email.'>' . "\r\n";
@mail($to,$subject,$htmlContent,$headers);
$succMsg = 'Your form have submitted successfully.';
else:
$errMsg = 'Invalid captcha value.';
endif;
else:
$errMsg = 'Please click on the reCAPTCHA box.';
endif;
else:
$errMsg = '';
$succMsg = '';
endif;
?>
We have made a contact frame with the new Google reCAPTCHA utilizing PHP. Investigate the demo of Google reCAPTCHA in PHP from the Demo connect. The reCAPTCHA joining procedure is given underneath.
Get reCAPTCHA api keys:
For adding reCAPTCHA to your site, you have to enlist your site and get reCAPTCHA API keys.
Enlist your site at Google from here – https://www.google.com/recaptcha/admin
Get your site key that's used to display the reCAPTCHA widget.
Get your Secret key helps authorizes communication between your site and the reCAPTCHA server.
Html Code :-
<script src="https://www.google.com/recaptcha/api.js" async defer></script>
<form action="" method="POST">
<input type="text" name="name" value="" />
<input type="text" name="email" value="" />
<textarea type="text" name="message"></textarea>
<div class="g-recaptcha" data-sitekey="9ABCGUTKOHdJnGhsKH--DDHD"></div>
<input type="submit" name="submit" value="SUBMIT">
</form>
Php Code :-
<?php
if(isset($_POST['submit']) && !empty($_POST['submit'])):
if(isset($_POST['g-recaptcha-response']) && !empty($_POST['g-recaptcha-response'])):
//your site secret key
$secret = '6LeXrhAUAAAAAMnkNXY0S8AyUI0vklLR5jaSfK5L';
//get verify response data
$verifyResponse = file_get_contents('https://www.google.com/recaptcha/api/siteverify?secret='.$secret.'&response='.$_POST['g-recaptcha-response']);
$responseData = json_decode($verifyResponse);
if($responseData->success):
//contact form submission code
$name = !empty($_POST['name'])?$_POST['name']:'';
$email = !empty($_POST['email'])?$_POST['email']:'';
$message = !empty($_POST['message'])?$_POST['message']:'';
$to = 'yogi.lalit2391@gmail.com';
$subject = 'New contact form have been submitted';
$htmlContent = "
<h1>Contact request details</h1>
<p><b>Name: </b>".$name."</p>
<p><b>Email: </b>".$email."</p>
<p><b>Message: </b>".$message."</p>
";
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";
$headers .= 'From:'.$name.' <'.$email.'>' . "\r\n";
@mail($to,$subject,$htmlContent,$headers);
$succMsg = 'Your form have submitted successfully.';
else:
$errMsg = 'Invalid captcha value.';
endif;
else:
$errMsg = 'Please click on the reCAPTCHA box.';
endif;
else:
$errMsg = '';
$succMsg = '';
endif;
?>
No comments:
Post a Comment