In this tutorial, we are going to integrate the Google reCAPTCHA in PHP. normally captcha is used for security purpose and only human users can pass through. computers or bots are not solving a captcha. so we will learn about captcha that how to integrate the Google reCAPTCHA in PHP.

First, we need to Google reCAPTCHA code library, site key and secret key. so first we go to Google reCAPTCHA panel and get site key and secret key.

After getting key, we will create a form using the bootstrap and pass the site key.

When the user fills the form data with resolves the reCAPTCHA code that time response token will be returned.


<!DOCTYPE html>
<html lang="en">
  <title>How to add google reCAPTCHA with php contact form</title>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="stylesheet" href="">
  <script src=""></script>
  <script src=""></script>
  <script src=''></script>
<div class="container">
   <form id="comment_form" action="check.php" method="post">
   <div class="form-group">
    <label for="name">Name:</label>
    <input type="text" class="form-control" id="name" name="name">

   <div class="form-group">
    <label for="email">Email address:</label>
    <input type="email" class="form-control" id="email" name="email">

   <div class="form-group">
      <div class="g-recaptcha" data-sitekey="here_enter_site_key"></div>
     <button type="submit" class="btn btn-default">Submit</button>

When the user submits the form that time sends the response token with form data. After checking the validation and return the final response.

    $name = $_POST['name'];
    $email = $_POST['email'];

    if(!empty($name) && !empty($email))

            echo '<h2>Please check the the captcha form.</h2>';

        $response = json_decode(file_get_contents("".$captcha."&remoteip=".$_SERVER['REMOTE_ADDR']), true);
        if($response['success'] == false)
            echo '<h2>something went wrong please try again</h2>';
            echo '<h2>Thanks for posting comment.</h2>';


If you like this article then You can see, comment and share this article demo

Show Demo