How To Add ReCaptcha In PHP Registration Form With Example


Today In this tutorial we will learn how to add Google CAPTCHA in PHP. In this post, we will show you google Recaptcha code in PHP project. We sometimes need Google reCaptcha like(registration, login, contact form).

And using the google reCaptcha with PHP HTML forms, you can easily validate your project and like contact, registration, login, and other form data in PHP.

How To add Google CAPTCHA in PHP HTML Form. In this post, we will show you How to Integrate Google reCAPTCHA with PHP HTML forms(registration, login, contact form).

simple captcha code in PHP learns this tutorial. Using the google ReCaptcha with PHP HTML forms, you can easily validate User Or robot in your forms like contact, registration, login, and other form data in PHP.

Spamming is one of the most common problems on the website. Then protect use google captcha code in PHP. Every site owner wants to get rid of spamming whether it is spam traffic or spam messages or spam comments on your site Then protect use simple captcha code in PHP.

Google’s reCAPTCHA is a new way to verify a User Or robot. learn this time google captcha code in HTML. Fill this form and click on I am not a robot then verify google reCAPTCHA with PHP. And it is very simple to verify the user. They just need a single click or tap to prove they are not a robot.

Use the below given easy steps to add reCaptcha in PHP HTML forms(registration, login, contact, etc.):

How To Add ReCaptcha In PHP Registration Form [ With Example ]
Step 1: Create Table In DB By Sql Query

Open your PHPMyAdmin and run the following SQL query. To create a table into your selected database:

CREATE TABLE `users1` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(30) NOT NULL,
  `email` varchar(100) NOT NULL,
  `password` varchar(250) NOT NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY `email` (`email`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
Step 2: Create A Database Connection PHP File

Create a file name db.php and add the following code into db.php file:

<?php
    $servername='localhost';
    $username='root';
    $password='';
    $dbname = "my_db";
    $conn=mysqli_connect($servername,$username,$password,"$dbname");
      if(!$conn){
          die('Could not Connect MySql Server:' .mysql_error());
        }
?>
Step 3: Register Your Website On Google Recaptcha 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.about:blank

Step 4: Create User Registration Form With Captcha Validation In PHP

In this step, create a user registration form that named registration.php and add google captcha validation.

So, open registration.php and add the following code into it:

<!doctype html>
<html lang="en">
   <head>
      <meta charset="utf-8">
      <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
      <title>How to Add Captcha in PHP Registration Form - phpcodingstuff.com</title>
      <!-- CSS -->
      <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css">
      <script src="https://www.google.com/recaptcha/api.js" async defer></script>
   </head>
   <body>
      <div class="container mt-5">
         <div class="card">
            <div class="card-header text-center">
               Add Google Captcha in PHP Registration Form
            </div>
            <div class="card-body">
               <form action="validate-captcha.php" method="post">
                  <div class="form-group">
                     <label for="exampleInputEmail1">Name</label>
                     <input type="text" name="name" class="form-control" id="name" required="">
                  </div>
                  <div class="form-group">
                     <label for="exampleInputEmail1">Email address</label>
                     <input type="email" name="email" class="form-control" id="email" aria-describedby="emailHelp" required="">
                     <small id="emailHelp" class="form-text text-muted">We'll never share your email with anyone else.</small>
                  </div>
                  <div class="form-group">
                     <label for="exampleInputEmail1">Password</label>
                     <input type="password" name="password" class="form-control" id="password" required="">
                  </div>
                  <div class="form-group">
                     <label for="exampleInputEmail1">Confirm Password</label>
                     <input type="password" name="cpassword" class="form-control" id="cpassword" required="">
                  </div>
                  <div class="g-recaptcha" data-sitekey="Your Site Key"></div>
                  <input type="submit" name="password-reset-token" class="btn btn-primary">
               </form>
            </div>
         </div>
      </div>
   </body>
</html>
Step 5: Create PHP File To Check Captcha And Save User Details

create validate-captcha.php file and add the following code into your validate-captcha.php file.

<?php
if(isset($_POST['submit']) &amp;&amp; $_POST['g-recaptcha-response']!="")
{
include "db.php";
$secret = 'You Site Key';
$verifyResponse = file_get_contents('https://www.google.com/recaptcha/api/siteverify?secret='.$secret.'&amp;response='.$_POST['g-recaptcha-response']);
$responseData = json_decode($verifyResponse);
if($responseData->success)
{
$name = $_POST['name'];
$email = $_POST['email'];
$pass = $_POST['password'];
mysqli_query($conn, "INSERT INTO users(name, email ,password) VALUES('" . $_POST['name'] . "', '" . $_POST['email'] . "', '" . md5($_POST['password']) . "')");
echo "User registration form with captcha validation has been successfully saved";
}
}
How To Add ReCaptcha In PHP Registration Form [ With Example ]
How To Add ReCaptcha In PHP Registration Form [ With Example ]
Important Note

Don’t forget to add site-key on registration.php form

Don’t forget to add site-secret on validate-captcha.php file.


I hope it can help you...

Leave a Reply

Your privacy will not be published. Required fields are marked *

We'll share your Website Only Trusted.!!