How to integrate Google Captcha in the contact form using PHP
Here I can show you how to include google captcha version 2.0 integration in the contact form.
Step 1:
Create captcha Account using https://www.google.com/recaptcha this link
Step 2 :
Register your Website address that mean website url.
Step 3:
Once you entered your website URL you will get secret key
Step 4: Next you need to integrate that captcha from your website there is two one once client side integration, Next Server site integration.Step 5: For Client side integration Copy phaste the code given by google like below screenshotPaste this snippet before the closing </head> tag on your HTML template:
<script src='https://www.google.com/recaptcha/api.js'></script>
Paste this snippet at the end of the <form> where you want the reCAPTCHA widget to appear:<div class="g-recaptcha" data-sitekey="Your Site Key"></div>
Step 6: Once you done step five you can see the captcha appear in your website. But you need to integrate server side using PHP or whatever you are using in your website.Here I can explain how to integrate using PHPWhen your users submit the form where you integrated reCAPTCHA, you'll get as part of the payload a string with the name "g-recaptcha-response". In order to check whether Google has verified that user, send a POST request with these parameters:
URL: https://www.google.com/recaptcha/api/siteverify
secret (required) | 6LdHGIYUAAAAADA67iyHZfcXvIvIBpWMZx0MBRfE |
response (required) | The value of 'g-recaptcha-response'. |
remoteip | The end user's ip address. |
This is the overall concept to integrate code in your website, Here i can give you full code to send mail.email,
<?php
header('Content-type: application/json'); $status = array( 'type'=>'success', 'message'=>'Thank you for contact us. We will get touch with you within 2 days ' );
// More headers$headers .= 'From: <info@sssss.com>' . "\r\n";$headers .= 'Cc: sssss@gmail.com' . "\r\n";
$name = @trim(stripslashes($_POST['name'])); $email = @trim(stripslashes($_POST['email'])); $subject = @trim(stripslashes($_POST['subject'])); $message = @trim(stripslashes($_POST['message'])); $phone = @trim(stripslashes($_POST['phone'])); $company = @trim(stripslashes($_POST['company'])); $service = @trim(stripslashes($_POST['service'])); ; $email_from = 'info@eightlegbuilders.com'; $email_to = 'ssss@gmail.com';//replace with your email $email_to = ''; if(isset($_POST['g-recaptcha-response']) && !empty($_POST['g-recaptcha-response'])) { if (strstr($_POST['email'], $hn)) { $secret = 'secretkey'; $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') { $allowed_hostnames = array("gmail.com", "yahoo.com"); $good_hostname = false; foreach ($allowed_hostnames as $hn) { if (strstr($_POST['email'], $hn)) { $body = 'Name: ' . $name . "\n\n" . 'Email: ' . $email . "\n\n" . 'Subject: ' . $subject . "\n\n" . 'Contact Number:' . $phone ."\n\n" . 'Company Name:' . $company ."\n\n". 'Message: ' . $message."\n\n". 'Job Required: ' .$service;
$success = mail($email_to, $subject, $body, $headers);
echo json_encode($status); die; }else{ if ($good_hostname==false) { // Keepin' it false here! $good_hostname = false; // Set it to false anyway } } } }}}
?>
<?php
header('Content-type: application/json'); $status = array( 'type'=>'success', 'message'=>'Thank you for contact us. We will get touch with you within 2 days ' );
// More headers$headers .= 'From: <info@sssss.com>' . "\r\n";$headers .= 'Cc: sssss@gmail.com' . "\r\n";
$name = @trim(stripslashes($_POST['name'])); $email = @trim(stripslashes($_POST['email'])); $subject = @trim(stripslashes($_POST['subject'])); $message = @trim(stripslashes($_POST['message'])); $phone = @trim(stripslashes($_POST['phone'])); $company = @trim(stripslashes($_POST['company'])); $service = @trim(stripslashes($_POST['service'])); ; $email_from = 'info@eightlegbuilders.com'; $email_to = 'ssss@gmail.com';//replace with your email $email_to = ''; if(isset($_POST['g-recaptcha-response']) && !empty($_POST['g-recaptcha-response'])) { if (strstr($_POST['email'], $hn)) { $secret = 'secretkey'; $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') { $allowed_hostnames = array("gmail.com", "yahoo.com"); $good_hostname = false; foreach ($allowed_hostnames as $hn) { if (strstr($_POST['email'], $hn)) { $body = 'Name: ' . $name . "\n\n" . 'Email: ' . $email . "\n\n" . 'Subject: ' . $subject . "\n\n" . 'Contact Number:' . $phone ."\n\n" . 'Company Name:' . $company ."\n\n". 'Message: ' . $message."\n\n". 'Job Required: ' .$service;
$success = mail($email_to, $subject, $body, $headers);
echo json_encode($status); die; }else{ if ($good_hostname==false) { // Keepin' it false here! $good_hostname = false; // Set it to false anyway } } } }}}
?>
0 comments:
Post a Comment
Thanks for your valuable feedback