Skip to Content
google recaptcha php example
php code / August 31, 2021

A website’s contact form is a must-have feature. By entering the form, the visitor can communicate with the site administrator. The contact request form is used for a variety of things, including submitting questions, recommendations, and requests. PHP makes it simple to create contact form functionality. In most cases, the admin receives the contact request data through email. PHP may be used to create a contact form with email capability.

The CAPTCHA feature is highly beneficial for preventing bots from filling out forms. To prevent spam submission, make sure the CAPTCHA validation is enabled on the contact form. The most successful solution for integrating CAPTCHA in the contact form is Google reCAPTCHA. This article will show you how to combine Google reCAPTCHA with PHP and construct a contact form.

Process of integrating a PHP contact form with Google reCAPTCHA:

Create reCAPTCHA API keys for Google.

To accept contact requests, create an HTML form.
Add a checkbox widget for reCAPTCHA to your contact form.

Use Google reCAPTCHA and PHP to validate form data.

PHP is used to send form data to an email address.

Create reCAPTCHA API keys for Google.

The Site key and Secret key are necessary to use the Google reCAPTCHA API before getting started. Get a site and secret keys by registering your site on the Google reCAPTCHA Admin panel. You may build a Google reCAPTCHA Site key and Secret key using a step-by-step approach.

Create a CAPTCHA-enabled HTML form
Create some HTML components that will receive user input. To submit a contact request, fill out the following HTML form’s input fields (Name, Email, Subject, and Message).

Add the reCAPTCHA widget from Google to your contact form:

Include the JavaScript library for reCAPTCHA.
To link the reCAPTCHA checkbox widget to the form, use the g-recaptcha tag element.
In the data-sitekey element, specify the reCAPTCHA API’s Site Key.

<!-- Google recaptcha API library -->
<script src="" async defer></script>
<!-- Status message -->
<?php if(!empty($statusMsg)){ ?>
<div class="status-msg <?php echo $status; ?>"><?php echo $statusMsg; ?></div>
<?php } ?>
<!-- Contact form fields -->
<form action="" method="post" class="cnt-form">
<div class="form-input">
<label for="name">Name</label>
<input type="text" name="name" placeholder="Enter your name" value="<?php echo !empty($postData['name'])?$postData['name']:''; ?>" required="">
<div class="form-input">
<label for="email">Email</label>
<input type="email" name="email" placeholder="Enter your email" value="<?php echo !empty($postData['email'])?$postData['email']:''; ?>" required="">
<div class="form-input">
<label for="subject">Subject</label>
<input type="text" name="subject" placeholder="Enter subject" value="<?php echo !empty($postData['subject'])?$postData['subject']:''; ?>" required="">
<div class="form-input">
<label for="message">Message</label>
<textarea name="message" placeholder="Type your message here" required=""><?php echo !empty($postData['message'])?$postData['message']:''; ?></textarea>
<div class="form-input">
<!-- Google reCAPTCHA box -->
<div class="g-recaptcha" data-sitekey="<?php echo $siteKey; ?>"></div>
<input type="submit" name="submit" class="btn" value="Submit">

The contact request data is sent to a server-side script (submit.php) for processing after the form is submitted. Add the server-side form submission script at the top of the file where the contact form sits.

<?php // Include form submission script 
include_once 'submit.php'; 

Submission of a contact form with reCAPTCHA verification (submit.php)
This file, submit.php, provides server-side code to handle form submission and contact request processing.

The PHP $_POST variable is used to retrieve data from form fields.
Check that the input fields are not empty and are filled.
The PHP FILTER VALIDATE EMAIL filter is used to validate email addresses.
Use the g-recaptcha-response POST parameter to validate the reCAPTCHA checkbox.
Verify reCAPTCHA response using PHP — Use the secret and response parameters to call the Google reCAPTCHA API.
secret – Enter the secret key here.

The contact request will be considered genuine and will proceed if the reCAPTCHA API provides a success answer.
Using the PHP mail() function, send contact form data to the admin through email.
The user sees a notice about the status of the contact form submission.

// Google reCAPTCHA API key configuration 
$siteKey = 'Insert_reCaptcha_Site_Key'; 
$secretKey = 'Insert_reCaptcha_Secret_Key'; 
// Email configuration 
$toEmail = ''; 
$fromName = 'Sender Name'; 
$formEmail = ''; 
$postData = $statusMsg = $valErr = ''; 
$status = 'error'; 
// If the form is submitted 
// Get the submitted form data 
$postData = $_POST; 
$name = trim($_POST['name']); 
$email = trim($_POST['email']); 
$subject = trim($_POST['subject']); 
$message = trim($_POST['message']); 
// Validate form fields 
$valErr .= 'Please enter your name.<br/>'; 
if(empty($email) || filter_var($email, FILTER_VALIDATE_EMAIL) === false){ 
$valErr .= 'Please enter a valid email.<br/>'; 
$valErr .= 'Please enter subject.<br/>'; 
$valErr .= 'Please enter your message.<br/>'; 
// Validate reCAPTCHA box 
if(isset($_POST['g-recaptcha-response']) && !empty($_POST['g-recaptcha-response'])){ 
// Verify the reCAPTCHA response 
$verifyResponse = file_get_contents(''.$secretKey.'&response='.$_POST['g-recaptcha-response']); 
// Decode json data 
$responseData = json_decode($verifyResponse); 
// If reCAPTCHA response is valid 
// Send email notification to the site admin 
$subject = 'New contact request submitted'; 
$htmlContent = " 
<h2>Contact Request Details</h2> 
<p><b>Name: </b>".$name."</p> 
<p><b>Email: </b>".$email."</p> 
<p><b>Subject: </b>".$subject."</p> 
<p><b>Message: </b>".$message."</p> 
// Always set content-type when sending HTML email 
$headers = "MIME-Version: 1.0" . "\r\n"; 
$headers .= "Content-type:text/html;charset=UTF-8" . "\r\n"; 
// More headers 
$headers .= 'From:'.$fromName.' <'.$formEmail.'>' . "\r\n"; 
// Send email 
@mail($toEmail, $subject, $htmlContent, $headers); 
$status = 'success'; 
$statusMsg = 'Thank you! Your contact request has submitted successfully, we will get back to you soon.'; 
$postData = ''; 
$statusMsg = 'Robot verification failed, please try again.'; 
$statusMsg = 'Please check on the reCAPTCHA box.'; 
$statusMsg = '<p>Please fill all the mandatory fields:</p>'.trim($valErr, '<br/>'); 
// Display status message 
echo $statusMsg;


Using HTML and PHP, this example script creates a simple contact form with CAPTCHA and email functionality. It aids in the integration of contact form functionality into a webpage while also protecting against bots and spam. This PHP contact form script can be used on any website, including one that is mobile responsive. Using HTML and PHP, this example script creates a simple contact form with CAPTCHA and email capability. It aids in the integration of contact form functionality into a webpage while also protecting against bots and spam. This PHP contact form script can be used on any website, including one that is mobile-friendly.

Annapa says:

Ηellо аll, guys! Ι know, mу mеѕsаge mаy be tоо ѕрecific,
Вut my sister found nісe mаn herе and theу mаrrіеd, ѕо hоw about mе?! 🙂
I am 26 уearѕ оld, Αnnа, from Romаnіa, Ι know Εnglіѕh аnd German lаnguаgeѕ аlѕo
Αnd… Ι hаvе sресіfiс dіseaѕе, namеd nymрhomаnіa. Ԝho know what iѕ thіs, саn undеrѕtаnd me (better tо sаy іt immеdiatеlу)
Ah yes, Ι соok vеrу tаѕtуǃ аnd I lovе nоt оnlу cook ;))
Im reаl gіrl, not рrоstіtutе, and lоoking for serіouѕ and hоt rеlаtiоnshіp…
Αnуwаy, you саn find mу profіlе here:

CatherinaLib says:

Ηellо all, guyѕǃ I know, mу mеѕsаge mаy be tоo ѕресіfic,
But mу ѕiѕtеr fоund niсе mаn herе аnd thеу marriеd, so hоw about mе?ǃ 🙂
I am 27 уеarѕ old, Сatherina, from Romaniа, I know Еnglіѕh and Gеrman languages also
And… I hаvе ѕpecіfiс dіsease, nаmеd nуmphomаniа. Who know what іs thіѕ, cаn understand mе (bеtter to ѕау іt immеdіаtеly)
Αh уеs, Ι соok vеry taѕtyǃ аnd Ι love nоt оnlу cоok ;))
Im real gіrl, nоt prоstіtutе, and lоokіng for serіouѕ and hot rеlаtionshiр…
Аnywаy, уоu can find my profile herе:

AlenaGymn says:

Нellо аll, guys! Ι know, mу mesѕage mаy bе tоо ѕpеcіfіс,
Βut mу ѕiѕter fоund nice mаn here and thеу mаrrіed, sо how аbout me?ǃ 🙂
Ι аm 27 уеarѕ old, Αlena, frоm Romania, Ι know Εngliѕh аnd Germаn lаnguаgeѕ аlѕo
Аnd… I havе ѕpеcifіс dіѕeаѕе, nаmed nymрhomaniа. Ԝho know whаt iѕ this, сan underѕtаnd mе (bеttеr to sау іt immеdіаtelу)
Аh уеs, Ι соok vеry tastуǃ аnd I lоvе nоt оnly cook ;))
Im rеаl girl, nоt рrоѕtіtutе, and lookіng for serіоuѕ аnd hоt relаtіоnship…
Anywау, yоu саn fіnd mу profіle here:

Leave a Reply

Your email address will not be published.

PHPCODE © 2023