How to display multiple recaptchas on a single page in PHP ?
Last Updated :
09 Dec, 2022
RECAPTCHA is a free service from Google that helps to protect websites from spam and abuse. A “CAPTCHA” is a Turing test to tell humans and bots apart. reCAPTCHA uses an advanced risk analysis engine and adaptive challenges to keep malicious software from engaging in abusive activities on your website.
In this article, we are going to discuss how to display multiple Google reCAPTCHA v2 on a single page in PHP.
Approach:
- Register your site at Google reCAPTCHA
- Submit the HTML form
- Get the response key on the server side
- Re-verify the key and give response to the user end.
Step 1: Register your site at Google reCAPTCHA — Register your website at Google reCAPTCHA to get the keys. The site key and server key are required to code the HTML form.
Click here to go to the Google reCAPTCHA website.
Step 2: Creating Google reCAPTCHA form in HTML — Create a Google reCAPTCHA form in HTML. We are going to create two forms, each with one input field and a button. Also, we will have an action page named action.php. To obtain reCAPTCHA in the HTML content, we must add a div element to the form and a Google reCAPTCHA CDN link to our HTML document.
CDN link : <script src=”https://www.google.com/recaptcha/api.js” async defer></script>
Div tag : <div class=”g-recaptcha” data-sitekey=”your_site_key”></div>
Note: You must substitute your Site Key for “your_site_key”.
Example:
HTML
<!DOCTYPE html>
< html lang = "en" >
< head >
< meta charset = "UTF-8" >
< meta http-equiv = "X-UA-Compatible"
content = "IE=edge" >
< meta name = "viewport"
content = "width=device-width, initial-scale=1.0" >
< link href =
rel = "stylesheet" >
< script src =
async defer></ script >
< title >Multiple Google reCAPTCHA</ title >
</ head >
< body >
< div class = "container" >
< h3 class = "text-center py-5" >
Multiple Google reCAPTCHA</ h3 >
< div class = "row" >
< div class = "col-lg-6 col-sm-12" >
< form class = "align-items-center"
action = "action.php"
method = "post" >
< div class = "mb-3" >
< input class = "form-control"
type = "text"
name = "name1"
id = "name1"
placeholder = "Enter Name"
required>
</ div >
< div class = "mb-3" >
< div class = "g-recaptcha"
data-sitekey = "your_site_key" ></ div >
</ div >
< button class = "btn btn-primary"
type = "submit"
name = "submit_btn1" >
Submit
</ button >
</ form >
</ div >
< div class = "col-lg-6 col-sm-12" >
< form action = "action.php"
method = "post" >
< div class = "mb-3" >
< input class = "form-control"
type = "text"
name = "name2"
id = "name2"
placeholder = "Enter Name"
required>
</ div >
< div class = "mb-3" >
< div class = "g-recaptcha"
data-sitekey = "your_site_key" ></ div >
</ div >
< button class = "btn btn-primary"
type = "submit"
name = "submit_btn2" >
Submit
</ button >
</ form >
</ div >
</ div >
</ div >
</ body >
</ html >
|
Step 3: PHP Back end — Now let’s code the action page.
The “isset()” method is used on the server side to verify whether a valid form was submitted after the form has been submitted. Following the validation, we retrieved the name using the $name variable and the Google ReCaptcha response using the $recaptcha variable.
PHP
if (isset( $_POST [ 'submit_btn1' ])) {
$name1 = $_POST [ 'name1' ];
$recaptcha1 = $_POST [ 'g-recaptcha-response' ];
}
if (isset( $_POST [ 'submit_btn2' ])) {
$name2 = $_POST [ 'name2' ];
$recaptcha2 = $_POST [ 'g-recaptcha-response' ];
}
|
Step 4: Verify the captcha – We must send a post request to the following URL in order to validate the captcha.
PHP
<? php
if (isset( $_POST [ 'submit_btn1' ])) {
$name1 = $_POST [ 'name1' ];
$recaptcha1 = $_POST [ 'g-recaptcha-response' ];
$secret_key = 'your_secret_key' ;
. $secret_key . '&response=' . $recaptcha1 ;
$response = file_get_contents ( $url );
$response = json_decode( $response );
if ( $response -> success == true) {
echo 'Google reCAPTACHA verified' ;
}
else {
echo 'Error in Google reCAPTACHA' ;
}
}
if (isset( $_POST [ 'submit_btn2' ])) {
$name2 = $_POST [ 'name2' ];
$recaptcha2 = $_POST [ 'g-recaptcha-response' ];
$secret_key = 'your_secret_key' ;
. $secret_key . '&response=' . $recaptcha2 ;
$response = file_get_contents ( $url );
$response = json_decode( $response );
if ( $response -> success == true) {
echo 'Google reCAPTACHA verified' ;
}
else {
echo 'Error in Google reCAPTACHA' ;
}
}
?>
|
Output:
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...