Spam और bots request से बचने के लिए Google ने reCAPTCHA v3 develop किया है जिसमें बिना user interaction के human और रोबोट को identify किया जा सकता है| इसमें score base पर work होता है| अगर score point ज्यादा रहा तो वह human के category में आएगा और score point कम रहा तो वह robot के category में आएगा| तो आज के इस tutorial में हम सीखेंगे की Google reCAPTCHA v3 को PHP में कैसे integrate करें?
हमने पिछले tutorial में same tutorial पढ़ा था लेकिन उसमें मैंने फॉर्म को Ajax की मदद से submit कराया था| कई बार ऐसा होता है की हमारा फॉर्म बिना Ajax की मदद से submit हो रहा होता है that means directly action attribute के द्वारा फॉर्म submit हो रहा होता है तो ऐसे में हमें Google reCAPTCHA implement करना मुश्किल हो जाता है| इसलिए मैंने यह tutorial खास उनलोगों के लिए बनाया है जो लोग form को बिना ajax की मदद से submit करवाना चाहते हैं|
Google reCAPTCHA spam filtering करने का सबसे popular और सबसे best solution है इसके द्वारा हम spam request को अपने फॉर्म को submit करने से रोक सकते हैं|
कई बार ऐसा होता है की जब हम बिना Google reCAPTCHA के form design करते हैं और उसको online अपलोड करते हैं तो हमारे पास बहुत सारे computer generated request आने लगते हैं जिससे की हमारा data भी बेवजह store होता है और साथ ही साथ बिना किसी reason के हमारे website पर bots ट्रैफिक (computer generated traffic) आते हैं जिससे की हमारा कोई फायदा नहीं होता है|
इसे रोकने के लिए हम Google reCAPTCHA का इस्तेमाल करते हैं|
Contents
Google reCAPTCHA implement करने के लिए क्या क्या जरुरी है?
अगर आप अपने website में या फिर अपने application में Google reCAPTCHA v3 को implement करना चाहते हैं तो इसके लिए आपके पास एक Gmail account होना जरुरी है और साथ ही साथ Google reCAPTCHA site key और secret key भी होना जरुरी है|
सबसे पहले हम सीखेंगे की Google reCAPTCHA site key और secret key कैसे प्राप्त करें? बहुत सारे लोगो का सवाल यह रहता है की क्या इसका API बिल्कुल free है| जी हाँ अभी इसका API बिल्कुल free है| यह पोस्ट 2020 में लिखा जा रहा है|
Register on Google reCAPTCHA site for site key and secret key
Site key और secret key पाने के लिए आपको सबसे पहले Google reCAPTCHA के website पर अपने Gmail account से register करना होगा|
Step 1: सबसे पहले Google reCAPTCHA के official website पर जाए वहां पर Admin console पर click करके अपने Gmail account से login करें|
Step 2: अब उसके बाद Right side में दिए गए Plus (+) icon पर click करके Register a new site box में अपना details fill करें|
- Label में आप अपने website से related कोई भी suitable नाम दे सकते हैं|
- reCAPTCHA type में reCAPTCHA v3 select करें|
- Domains में आप अपने website का URL type करें| इसमें आप एक से ज्यादा website का URL add कर सकते हैं| एक बात का हमेशा ध्यान रखें की कभी भी http या https या WWW URL में add ना करें| अगर आप localhost में reCAPTCHA add कर रहे हैं तो इसके लिए localhost और 127.0.0.1 URL add करें|
- Accept the reCAPTCHA terms of service को tick करें|
- अब Submit करें|

Step 3: इसके बाद आपको site key और secret key मिल जायेगा|

अब इसके बाद आपको एक एक करके client side और server side दोनों ही side Google reCAPTCHA को integrate करना होगा| सबसे पहले हम client side integration देखेंगे उसके बाद उसे server side integrate करके वेरीफाई करेंगे|
Client side integration of Google reCAPTCHA
Client side integration के लिए Google के द्वारा एक API दिया गया है जिसे आपको अपने website में integrate करना होगा|
https://www.google.com/recaptcha/api.js?render=put your site key here
इसमें Put your site key here के जगह पर आपको अपना site key add करना होगा| अब आप जब अपने webpage को browser में open करेंगे तो आप Google reCAPTCHA का icon सबसे निचे right side में देख सकते हैं| अगर reCAPTCHA ICON दिख रहा है तो मान लीजिये की आपका first step सही से integrate हुआ|
इसके बाद दूसरा step में कुछ JavaScript code दिए गए हैं जो की एक token generate करने का काम करता है| इस code को आपको उस page पर रखना है जहाँ पर आप reCAPTCHA implement कर रहे हैं|
<script>
grecaptcha.ready(function() {
grecaptcha.execute('put your site key here', {action: 'homepage'}).then(function(token) {
// send the token to the back end script for verification
});
});
</script>
Full code with client and server side integration
index.php
<!DOCTYPE html>
<html>
<head>
<title>Google Recaptcha V3</title>
</head>
<body>
<h1>Google Recaptcha V3</h1>
<form action="recaptcha.php" method="post">
<label>Name</label>
<input type="text" name="name" id="name">
<input type="hidden" name="token" id="token" />
<input type="hidden" name="action" id="action" />
<input type="submit" name="submit">
</form>
<script src="https://www.google.com/recaptcha/api.js?render=put your site key here"></script>
<script src="https://code.jquery.com/jquery-3.4.1.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
setInterval(function(){
grecaptcha.ready(function() {
grecaptcha.execute('put your site key here', {action: 'application_form'}).then(function(token) {
$('#token').val(token);
$('#action').val('application_form');
});
});
}, 3000);
});
</script>
</body>
</html>
recaptcha.php
<?php
if ($_POST['submit']) {
$name = $_POST['name'];
$token = $_POST['token'];
$action = $_POST['action'];
$curlData = array(
'secret' => 'put your secret key here',
'response' => $token
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://www.google.com/recaptcha/api/siteverify");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($curlData));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$curlResponse = curl_exec($ch);
$captchaResponse = json_decode($curlResponse, true);
if ($captchaResponse['success'] == '1' && $captchaResponse['action'] == $action && $captchaResponse['score'] >= 0.5 && $captchaResponse['hostname'] == $_SERVER['SERVER_NAME']) {
echo 'Form Submitted Successfully';
} else {
echo 'You are not a human';
}
}
code description
यहाँ पर मैंने दो page create किया है पहला page index.php है और दूसरा page recaptcha.php है|
index.php में हमने एक simple form बनाया है| जिसमें मैंने एक input type text और दो input type hidden field लिया| Text field हमें browser में दिखेगा जिसमें हम अपने text enter करेंगे और बाकि के दो hidden field को हमने Google reCAPTCHA verify करने के लिए है जिसमें की token और action store करके हम back end तक send करेंगे|
सबसे निचे में हमने दो script add किया है पहला script Google reCAPTCHA के API का है और दूसरा jQuery का CDN है| उसके बाद 3 second के interval पर मैंने Google reCAPTCHA को execute किया है और फिर action और token को 3 seconds के अंतराल पर set किया है|
यह इसलिए किया गया है क्योंकि अगर कोई user उस फॉर्म को बहुत देर के बाद submit करता है तो उसका टोकन expire ना हो हमेशा नया टोकन generate होते रहे| इसे आप अपने अनुसार change कर सकते हैं|
दूसरा page recaptcha.php है जिसमें की हमने name, token और action को receive किया है और फिर Google reCAPTCHA server side integration cURL के द्वारा किया है|
cURL execute होने के बाद हमें कुछ इस प्रकार का response मिलेगा जैसा की निचे दिया गया है|
{
"success": true|false, // whether this request was a valid reCAPTCHA token for your site
"score": number // the score for this request (0.0 - 1.0)
"action": string // the action name for this request (important to verify)
"challenge_ts": timestamp, // timestamp of the challenge load (ISO format yyyy-MM-dd'T'HH:mm:ssZZ)
"hostname": string, // the hostname of the site where the reCAPTCHA was solved
"error-codes": [...] // optional
}
इसमें हमें success true या false मिलेगा| अगर success true मिलता है तो हम आगे का condition check करेंगे|
दूसरा हमें score मिलेगा| Score 0.0 से लेकर के 1.0 तक हो सकता है अगर score 0.5 या उससे ज्यादा हुआ तो human के category में आता है अथवा रोबोट के category में आएगा|
तीसरा हमें action मिलेगा| इस action को हम अपने वाले action जो की हम फॉर्म से receive किये थे उससे compare करेंगे अगर दोनों same हुआ तो ही आगे का process करेंगे| यह इसलिए मिलता है ताकि कोई बिच में हमारे request को change ना कर दे|
फिर हमें challenge_ts मिलता है इसमें हमे time मिलता है की कब reCAPTCHA verify हुआ|
फिर हमें host-name मिलता है| इसमें हमारा website का URL मिलता है that means जिस website से हमने captcha verify करने के लिए request किया था उसी का response मिलता है| यह इसलिए होता है जिससे की हम ये पता लगा सके की हमारे website का ही score मिला है या नहीं|

Read Also:
Your post is very good due to which I got this information