PHP Contact Form with Google reCaptcha V2

Last Updated on March 5, 2023 by Roshan Parihar

PHP contact form with Google reCaptcha is the most useful feature to protect your forms from spam bots. If you want to restrict spam submissions on your website, it can be the most effective solution for you.

Google reCaptcha can help you validate humans and robots when they submit the contact form. Integration with Google reCaptcha is the most recommended solution to stop spam and robot submissions.

How to Create PHP Contact Form with Google reCaptcha

If you are continuously getting spam emails from your contact forms, you must implement Google reCaptcha protection. Users just have to click the checkbox to confirm that they are humans and not robots.

In this post, you will learn how to generate the Google reCaptcha site key and secret key. You will also learn a step-by-step process on how to validate humans and robots with it on your PHP contact form. After the identity is confirmed that the user is human, the form will send emails.

So, let’s get started.

How to Create PHP Contact Form with Google reCaptcha (Step-by-step)

Here is the step-by-step process on how to create a contact form with Google Recaptcha in PHP:-

Step 1: Generate Google reCaptcha V2 API Keys

First of all, you need to create Site Key and Secret Key to make your Google reCaptcha work for your contact form.

Get the Site Key and Secret Key From Google reCaptcha V2

To get the keys from Google reCaptcha v2, you have to visit the Google reCaptcha admin panel and log in to your Google account.

After logging in, you will get the settings page where you have to make the following setting to get the Site key and Secret key:-

  • Label: Add a name to identify your registration for Google Recaptcha. It can be the name of your domain to easily identify that you are creating this for the specified domain.
  • reCaptcha type: Select version 2 (reCaptcha v2). The ‘I’m not a robot’ tickbox is pre-selected here.
  • Domains: Add a domain name where you want to display the Google reCaptcha in forms. Press enter to make it selected.
  • Owners: Your Google email address is already showing selected here.
  • Accept reCaptcha Terms of Service: Click the checkbox that you agree to the Google API terms of use.
  • Send alerts to owners: Click the checkbox if you want to get alerts on your submission.

When you complete the above fields, click the ‘Submit’ button to save your settings.

generate google recaptcha keys

You will get the Google reCaptcha v2 Site key and Secret key as shown in the image below.

Site and Secret Keys

Let’s store the above site and secret in a separate PHP variable to use in your contact form.

Step 2: Create PHP Contact Form Design with Google reCaptcha

After creating and obtaining your Google reCaptcha keys, you have to create your contact form design. The below HTML code is the main structure of the contact form.

Starting from the top of the structure, it contains the Google reCaptcha API URL. After that, it contains the form with form fields like Name, Email, and Message. If you want to know the below form structure in detail, you can read our post on Simple HTML Contact Form.

The form also contains the Google reCaptcha <div> with the site key just before the submit button. This is required to display the captcha inside the form.

HTML Code for PHP Contact Form with Google reCaptcha

Also, you have to place the previous step obtained site and secret keys in the indicated place below. The above form also indicated the place where you have to place the PHP code given in the last step.

CSS Code for PHP Contact Form with Google reCaptcha

The above HTML structure of the contact form is not enough to make a better design. To make it looks better to your audience. Add some CSS code before the closing <head> tag.

The below code contains CSS for all the form fields including the button element with error and success message CSS. However, it does not require adding CSS for the captcha code as it is standard and already designed beautifully by the Google team.

After you add the above CSS to your contact form, it converts into a beautiful-looking contact form as shown in the image below. It also displays the Google reCaptcha to identify humans and robots.

PHP Contact Form with Google reCaptcha

Let’s add some PHP code to validate the form and Google reCaptcha to protect your form from spam bots.

Step 3: Send Email with Google reCaptcha Validation

The PHP code should validate the Google reCaptcha to stop spam bots. It should not allow users to submit the form without clicking the Captcha. The PHP code should also validate the email address to check if the format is correct or not.

The PHP code first gets the form data using the $_POST global variable. After that, it validates the email address using the regex expression and the preg_match().

To validate the Google Recaptcha of the PHP contact form, you have to use the $_POST global variable and get the Captcha field using g-recaptcha-response. You have to check whether the captcha fields isset and are not empty to find whether users clicked it or not.

It only allows the further code to execute when getting the success validation response of Google Recaptcha. The below code is well commented to easily understand each part of the code. So, see the below PHP code to understand the use of the method.

Click the button given below to download the PHPMailer library whose link is given below. It is required to make the below code work to send emails as the below code uses the PHPMailer to send emails. It is the fastest and easiest way to send emails in PHP.

Download PHPMailer

The above PHP code uses the PHPMailer of PHP to send emails with the user’s data entered into the contact form. If you want to use the mail function of PHP, you can read our post on PHP code for the contact form to send emails using mail()

Leave a comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.