How To Add New Google Recaptcha In PHP

By : phpicoder Created : March 5, 2021

in this tutorial we are going to give you very simple example of how to add google reCAPTCHA code in your php website. I will tell you how to add new google recaptcha in php website, I will explain you step by step tutorial of add google reCAPTCHA v2 code in php application using api key.

In this post, we are going to learn about Google new recaptcha API. This protects your website for spammers and robots, in this post I had implemented new reCaptch API demo with HTML Signup Form using PHP. I like the new design it is clean and impressive, hope you will like it. Please take a look at the below example step by step.

How To Add New Google Recaptcha In PHP

What is reCAPTCHA?

reCAPTCHA is a free service from Google that helps protect websites from spam and abuse. A “CAPTCHA” is a test to tell human and bots apart.

Step 1: Get reCAPTCHA API keys:

first require to generate 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 :

google recaptcha admin page

after sucessfully register you can get site key and secret key from Google like bellow preview.

google recaptcha get key

Step 2: Create a Database :

  • Open phpmyadmin
  • Create database as "tutorial"
  • Click create table and name it as "user"
  `id` int(11) NOT NULL,
  `user_name` varchar(25) NOT NULL,
  `email` varchar(50) NOT NULL,
  `password` varchar(255) NOT NULL

Step 3 : Connect a database

In this step we are going to connect the database with our project.


$servername = "localhost";
$username = "root";
$password = "";
$dbname = "tutorial";

  // Create connection
  $conn = new mysqli($servername, $username, $password, $dbname);

  // Check connection
  if ($conn->connect_error) 
      die("Connection failed: " . $conn->connect_error);

Step 4: Signup Form View Form

<script src=''></script>

Then Just simply change the Secret-Key in data-sitekey attribute in Signup.php file. It will display the captcha component on your form.


<!DOCTYPE html>
<html lang="en">
  <title>New Google Recaptcha In PHP - phpicoder</title>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="stylesheet" href="">
  <script src=''></script>
  <div class="container">
  <div class="jumbotron">
    <h2 class="text-center">New Google Recaptcha In PHP - phpicoder</h2>
    <form class="form-horizontal" action="action_page.php">
      <div class="form-group">
        <label class="control-label col-sm-2" for="name">Name:</label>
        <div class="col-sm-10">
          <input type="text" class="form-control" id="name" placeholder="Enter name" name="name">
      <div class="form-group">
        <label class="control-label col-sm-2" for="email">Email:</label>
        <div class="col-sm-10">
          <input type="email" class="form-control" id="email" placeholder="Enter email" name="email">
      <div class="form-group">
        <label class="control-label col-sm-2" for="password">Password:</label>
        <div class="col-sm-10">          
          <input type="password" class="form-control" id="password" placeholder="Enter password" name="password">
      <div class="form-group">
        <label class="control-label col-sm-2" for="recaptcha">Recaptcha:</label>
        <div class="col-sm-10">          
          <div class="g-recaptcha" data-sitekey="******************************"></div>
      <div class="form-group">        
        <div class="col-sm-offset-2 col-sm-10">
          <button type="submit" class="btn btn-default">Submit</button>

Step 3: Backend  Action File

This file will check weather you are robot or not, If robot, then redirects signup page, otherwise make you proceed

Now create another file named action_page.php and paste bellow code in that file



//your site secret key
$secret = '6LcuCSwUAAAAAEPl2fomdd0roFcNwLu_eWJ39CNO';

//get verify response data
$verifyResponse = file_get_contents(''.$secret.'&response='.$_POST["g-recaptcha-response"]);

$responseData = json_decode($verifyResponse);

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

    //Encrypt hash passwords
    $options = ['cost' => 12];
    $password = password_hash($_POST['password'], PASSWORD_DEFAULT, $options);

    if($data=$conn->prepare("INSERT INTO user (user_name,email,password) VALUES(?,?,?)"))

	// Bind the variables to the parameter as strings. 
	$data->bind_param("sss", $name,$email,$password);

	// Execute the prepared statement.

	// Close the prepared statement.

	$_SESSION['msg']="Add Data Seccess...";
	$_SESSION['msg']="Dada not protected please try again...";
    $_SESSION['msg']="You are a robot...";
header("location: Signup.php");

That's it! The new google reCAPTCHA code is ready to post in your signup or signin form. Just copy the code and paste in appropriate file. If you need any help, please contect me.

I hope it can help you for new google recaptcha in php tutorial...