How To Add Google Recaptcha With PHP

How To Add Google Recaptcha With PHP

How To Add Google Recaptcha With PHP

0 Sales


The new reCAPTCHA v2 has been released by Google. Using the reCAPTCHA app, users can claim to be human without a CAPTCHA problem being solved. To verify that they are not a robot, you only need a single click. With the better user experience, the Google reCAPTCHA checkbox defends the website from spam. In general, the functionality of Google reCAPTCHA is used for the web form. However, it is possible to use reCAPTCHA to validate any response from the website.


Google reCAPTCHA v2 offers an easy way to prevent spam from getting your web form or web page

 Using PHP, Google can easily integrate reCAPTCHA into the website. We'll teach you in this tutorial how to incorporate Google's reCAPTCHA v2 checkbox in the web form with PHP.


The following features will be incorporated in the sample software to illustrate the integration of Google reCAPTCHA with PHP.


  • To submit an application for contact, create an HTML form.

  • Fill in the form with a reCAPTCHA checkbox widget.

  • Verifying Google's reCAPTCHA API response.

  • Use PHP to retrieve data from the form and send email.


Generate reCAPTCHA API Keys

Using the Google reCAPTCHA API requires the reCAPTCHA buttons. You need to register your domain and get reCAPTCHA API keys before you add reCAPTCHA v2 checkbox to your app.

Register Website:

Register the domain of your website at Google reCAPTCHA Admin console.

  • Label – In the future, it will help identify your registered site. 

  • ReCAPTCHA type – Select reCAPTCHA v2 » I'm not a 

  • Domain Checkbox robot – Specify your website's domain.


Get Site Key and Secret Key:


Your site will be added after the submission and the keys to the reCAPTCHA will be generated. When calling Google's reCAPTCHA API, the Site Key and Secret Key must be specified in the script.

  • Site Key-This key is used in the reCAPTCHA widget's HTML code.

  • Secret Key–This key enables communication between your site and the reCAPTCHA server to be authorized.

Add reCAPTCHA Widget to HTML Form

At first, include the JavaScript library of reCAPTCHA API.

<script src="" async defer></script>


Add the component g-recaptcha in the HTML form where the reCAPTCHA checkbox widget is to be displayed.


The DIV element g-recaptcha has a class (named 'g-recaptcha') and the attributes of data-sitekey.

The reCAPTCHA API Site Key will be specified in the attribute data-sitekey.


<!-- Status message -->

<?php if(!empty($statusMsg)){ ?>

    <p class="status-msg <?php echo $status; ?>"><?php echo $statusMsg; ?></p>

<?php } ?>

<form action="" method="post">

    <!-- Form fields -->

    <div class="input-group">

        <input type="text" name="name" value="<?php echo !empty($postData['name'])?$postData['name']:''; ?>" placeholder="Your name" required="" />


    <div class="input-group">    

        <input type="email" name="email" value="<?php echo !empty($postData['email'])?$postData['email']:''; ?>" placeholder="Your email" required="" />


    <div class="input-group">

        <textarea name="message" placeholder="Type message..." required="" ><?php echo !empty($postData['message'])?$postData['message']:''; ?></textarea>



    <!-- Google reCAPTCHA box -->

    <div class="g-recaptcha" data-sitekey="Your_reCAPTCHA_Site_Key"></div>


    <!-- Submit button -->

    <input type="submit" name="submit" value="SUBMIT">



Verify reCAPTCHA Response (Server-side Validation)


The input data will be sent to the server-side script after submission of the form to verify the response of the client and process the request for touch.


  • Validate the fields of the form to verify if the user fills the fields of the data.

  • Use the POST parameter g-recaptcha-response to check whether the user selects the checkbox for reCAPTCHA.

  • Verify the difficulty of reCAPTCHA with reCAPTCHA and PHP.

  • Call the Google reCAPTCHA API and pass the Hidden Site Key & User Response. Check the reCAPTCHA response.

  • If the reCAPTCHA response is correct and accurate, the contact form information using PHP will end an email to the site administrator.

  • The status message is displayed to us



$postData = $statusMsg = ''; 

$status = 'error'; 


// If the form is submitted 


    $postData = $_POST; 


    // Validate form fields 

    if(!empty($_POST['name']) && !empty($_POST['email']) && !empty($_POST['message'])){ 


        // Validate reCAPTCHA box 

        if(isset($_POST['g-recaptcha-response']) && !empty($_POST['g-recaptcha-response'])){ 

            // Google reCAPTCHA API secret key 

            $secretKey = 'Your_reCAPTCHA_Secret_Key'; 


            // 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 


                // Posted form data 

                $name = !empty($_POST['name'])?$_POST['name']:''; 

                $email = !empty($_POST['email'])?$_POST['email']:''; 

                $message = !empty($_POST['message'])?$_POST['message']:''; 


                // Send email notification to the site admin 

                $to = '[email protected]'; 

                $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> 



                // 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:'.$name.' <'.$email.'>' . "\r\n"; 


                // Send email 



                $status = 'success'; 

                $statusMsg = 'Your contact request has submitted successfully.'; 

                $postData = '';


                $statusMsg = 'Robot verification failed, please try again.'; 



            $statusMsg = 'Please check on the reCAPTCHA box.'; 



        $statusMsg = 'Please fill all the mandatory fields.'; 





You can use it for personal or commercial projects. You can't resell it partially or in this form.


Create Date : May 15, 2020

Updated Date : Jun 17, 2021


Comments : 0

Downloads : 0