How to create recaptcha validation before submit in php

Welcome you to the article “ recaptcha validation before submit ”. The guidance of this article is on PHP source code. Besides, after reading this article, you could download the source code of this article. You can use it without minding.

recaptcha validation before submit

Introduction

We need to use recaptcha validation before submit because it helps make sure that there is a human wanting to submit information. And they are people to whom we need to reply. In addition, captcha can be used to avoid being spammed or DDOS attack.

Preparation

In this article, the first thing we need is an editor software such as notepad ++ or phpdesigner as I am using. You can use any tool that is suitable for you.

The second thing is a Google account. It is your Gmail and password.

Besides, you also need hosting. Previously, you can use localhost. But recently, Google API does not support localhost anymore. There will be a notification as below. You can try to see if your computer allows to use localhost.

recaptcha validation before submit

Implementation

Create php source code

To save time, I will provide you with the source code of the article. You just need to copy and edit according to your need,

Create index.php file to which you add the code

<html lang="en">
<head>
<title>recaptcha validation before submit</title>
<scriptsrc='https://www.google.com/recaptcha/api.js'></script>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css">
</head>
<body>
 
<div class="container">
<div class="row">
<div class="col-xs-8">
<div class="panel panel-primary">
<div class="panel-heading"><strong>Precaptcha validation before submit</strong></div>
<div class="panel-body">
<form action="do_process.php" class="form-horizontal" method="POST">
 
<div class="form-group">
<label class="col-xs-4 control-label">Full Name:</label>
<div class="col-xs-6">
<input type="text" class="live-form" name="name" placeholder="Full Name" required="" />
</div>
</div>
 
<div class="form-group">
<label class="col-xs-4 control-label">Your valid Email:</label>
<div class="col-xs-6">
<input type="text" class="live-form" name="email" placeholder="Your Email" required="" />
</div>
</div>
<div class="form-group">
<label class="col-xs-4 control-label">comments:</label>
<div class="col-xs-6">
<textarea type="text" class="live-form" name="comments" placeholder="Your comments" required=""></textarea>
</div>
</div>
<div class="form-group">
<label class="col-xs-4 control-label">Simple Captcha:</label>
<div class="col-xs-6">
<div class="g-recaptcha" data-sitekey="6LflqWkUAAAAACBi3OmE825ELQ9ZkeINwRsEITpa"></div>
</div>
<div class="form-group">
<div class="col-xs-6 col-xs-offset-6" class="live-center">
<br/>
<input class="btnbtn-success" type="submit" name="submit" value="Submit">
</div>
</div>
</form>
</div>
</div>
</div>
</div>
</div>
</body>
</html>
You should pay attention to this code <div class="g-recaptcha" data-sitekey="6LflqWkUAAAAACBi3OmE825ELQ9ZkeINwRsEITpa"></div>
You should replace the sitekey with your code. To get this code, you should view the sitekey and secret key.
<?php
if($_SERVER["REQUEST_METHOD"] === "POST")
{ 
$google_recpachscrkey = "6LflqWkUAAAAAMEt_pgOdo9IucoBDTTq77K8Anlq";
$response = file_get_contents("https://www.google.com/recaptcha/api/siteverify?secret=".$google_recpachscrkey."&response=".$_POST['g-recaptcha-response']);
$response = json_decode($response, true);
if($response["success"] === true){
echo "USER Form Submit Successfully.";
}else{
echo "You Simple are a robot";
}
}

Please pay attention to this code $google_recpachscrkey=””, please view steps on how to create site key and secret key to add information to.
Create site key and secret key
Firstly, you get access to this link https://www.google.com/recaptcha/admin#list, then you log in your Google account.
Label: here you enter the title of the website for more convenient administration if you have many projects.
Choose the type of reCAPTCHA: you choose reCAPTCHA v2
Domain: you input the domain. If you have lots of domains, you should write each domain in one line, please delete prefix http:// and www.

Then you click on Accept the recaptcha Terms of Service checkbox

recaptcha validation before submit

The next page will include lots of information as well as user guide. But in this article, you just need to pay your biggest attention to the site key and secret key. You should copy and add this information to the part above.

recaptcha validation before submit

You have read the article recaptcha validation before submit. Now you can know one more data validation before processing. You can add/delete fields in php form. I have used Custom field module and found it very interesting. You can add unlimited fields to the registration page or checkout page pretashop. Of course, the module supports all types of data including recaptcha. If you are interested in the module, you can refer to this link https://addons.prestashop.com/en/registration-ordering-process/27422-custom-fields-registration-form-and-checkout.html Thanks for following the article.

Enjoy this blog? Please spread the word :)