All TalkersCode Topics

Follow TalkersCode On Social Media

devloprr.com - A Social Media Network for developers Join Now ➔

Create Signup Form With Google reCAPTCHA Using PHP

Last Updated : Jul 1, 2023

IN - HTML PHP MySQL | Written & Updated By - Amruta

In this tutorial we will show you how to create signup form with google recaptcha using PHP and MySQL, Google reCAPTCHA is a new kind of captcha system introduced to increase form security and prevent spamming completely you can easily add Google reCAPTCHA to your website.

You may also like captcha system on signup using PHP.

Create Signup Form With Google reCAPTCHA Using PHP

To Create Signup Form With Google reCAPTCHA It Takes Only Three Steps:-

  1. Register your website and get all details
  2. Make a HTML file and define markup for signup form
  3. Make a PHP file to check captcha and store details

Step 1. Register your website and get all details

To add google recaptcha to your website you need to register your website here https://www.google.com/recaptcha/admin. Then get your site key and secret key.

Step 2. Make a HTML file and define markup for signup form

We make a HTML file and save it with a name signup.html

<html>
<head>
<script src="https://www.google.com/recaptcha/api.js" async defer></script>
</head>
<body>
<div id="wrapper">
 <form action="do_signup.php" method="POST">
  <input type="text" name="name" value=""/>
  <input type="text" name="email" value=""/>
  <input type="password" name="password"/>
  <div class="g-recaptcha" data-sitekey="Your Site Key"></div>
  <input type="submit" name="submit" value="SUBMIT">
 </form>
</div>
</body>
</html>

In this step we create a form for user signup and add Google reCAPTCHA div in our form just place your site key in 'data-sitekey' and we also add api.js javascript file for captcha given by google.

Step 3. Make a PHP file to check captcha and store details

We make a PHP file and save it with a name do_signup.php

// Database Structure 
CREATE TABLE `users` (
 `id` int(11) NOT NULL AUTO_INCREMENT,
 `name` text NOT NULL,
 `email` text NOT NULL,
 `password` text NOT NULL,
 PRIMARY KEY (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=5 DEFAULT CHARSET=latin1

<?php

if(isset($_POST['submit']) && $_POST['g-recaptcha-response']!="")
{
 $secret = 'You Site Key';
 $verifyResponse = file_get_contents('https://www.google.com/recaptcha/api/siteverify?secret='.$secret.'&response='.$_POST['g-recaptcha-response']);
 $responseData = json_decode($verifyResponse);
 if($responseData->success)
 {
  $name = $_POST['name'];
  $email = $_POST['email'];
  $pass = $_POST['password'];
  
  $host="localhost";
  $username="root";
  $password="";
  $databasename="sample";

  $connect=mysql_connect($host,$username,$password);
  $db=mysql_select_db($databasename);

  mysql_query("insert into users values('','$name','$email','$pass')");
  echo "Successfully Signup";
 }
}

In this step we create a table called 'users' to store users data after signup then check the captcha using google link by passing site key and captcha and if the entered captcha is correct we store the user details in our database.

You may also like login with google using PHP.

Thats all, this is how to create signup form with google reCAPTCHA using PHP and MySQL. You can customize this code further as per your requirement. And please feel free to give comments on this tutorial.

I hope this tutorial on google reCAPTCHA form helps you and the steps and method mentioned above are easy to follow and implement.