PHP Google Recaptcha V2 Example Tutorial

By Hardik Savani November 5, 2023 Category : PHP

Hi Dev,

This simple article demonstrates of php google recaptcha v2 example. we will help you to give example of how to add google captcha v2 in php registration form. We will look at example of how to add google recaptcha v2 in php. We will look at example of google recaptcha v2 php validation. follow bellow step for google recaptcha v2 server side validation php.

As we know, When we created contact us form or feedback form or any form that access by publicly Then we have so many submission from spam. They submit those form using curl request or something. So we can not get real user from those forms. Here, i we will use google recaptcha v2 to prevent spam submission. we will create contact us form and use google recaptcha v2 and prevent them. So that way you have only real users and real feedback you will get.

I will give you step of how to implement google recaptcha v2 code in php, so let's follow below step to done this example:


Step 1: Add Google API Key

In this step we need to set google site key and secret key. If you don't have site key and secret key then you can create from here. First click on this link : Recaptcha Admin

Ok, after successfully register you can get site key and secret key from like bellow preview.

Next, you need to copy Site Key and Secret Key. we will use this keys on next step:

Step 2: Create index.php File

In this step, copy bellow code and replace {GOOGLE-SITE-KEY} into Google Site Key replace there.




<title>PHP Google reCAPTHA V2 Example -</title>

<link href="" rel="stylesheet" integrity="sha384-EVSTQN3/azprG1Anm3QDgpJLIm9Nao0Yz1ztcQTwFspd3yD65VohhpuuCOmLASjC" crossorigin="anonymous">

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



<div class="container">

<h1>PHP Google reCAPTHA V2 Example -</h1>

<form action="process.php" method="post">

<div class="mb-3">

<label for="exampleInputEmail1" class="form-label">Email address:</label>

<input type="email" class="form-control" id="exampleInputEmail1" aria-describedby="emailHelp">


<div class="mb-3">

<label class="form-label">Comment:</label>

<textarea class="form-control"></textarea>


<div class="mb-3">

<div class="g-recaptcha" data-sitekey="{GOOGLE-SITE-KEY}"></div>


<div class="mb-3">

<button type="submit" class="btn btn-primary">Submit</button>






Step 3: Create process.php File

In this step, copy bellow code and replace {GOOGLE-SECRET-KEY} into Google Secret Key replace there.





$recaptcha_secret = "{GOOGLE-SECRET-KEY}";

$response = file_get_contents("".$recaptcha_secret."&response=".$_POST['g-recaptcha-response']);

$response = json_decode($response, true);

if($response["success"] === true){

echo "Form Submit Successfully.";


echo "You are a robot";



Run PHP App:

All the required steps have been done, now you have to type the given below command and hit enter to run the PHP app:

php -S localhost:8000

Now, Go to your web browser, type the given URL and view the app output:


I hope it can help you...

Tags :