To integrate reCAPTCHA in your HTML form, you need a public & secret key.
You can get the both keys from this URL https://www.google.com/recaptcha/admin and it will look like
- Public Key: 9Madr_cSZZADgLy9zeFTEraGn9ogHhxuh_6pquJUHYyt
- Secret Key: 9Madr_cSZZARuPr8duJUEyaYn3ogDherh-5pqu
You need to implemented the public key at client side i.e HTML page & secret at server side i.e. PHP Script.
Here is the code.
HTML Form:
<script src='https://www.google.com/recaptcha/api.js'></script><form>
First name:
<input type="text" name="firstname">
Last name:
<input type="text" name="lastname">
Captcha
<div class="g-recaptcha" data-sitekey="paste_your_recaptcha_public_key_here"></div>
</form>
Server Side Script:
$fileContent = '';
if (isset($_REQUEST['g-recaptcha-response']) && !empty($_REQUEST['g-recaptcha-response'])) {
$fileContent = file_get_contents("https://www.google.com/recaptcha/api/siteverify?secret=paste_your_recaptcha_secret_key_here&response=". $_REQUEST['g-recaptcha-response']);
}
$jsonArray = json_decode($fileContent, true);
if (isset($jsonArray['success']) && $jsonArray['success']==true) {
// process your logic here
} else {
echo 'Invalid verification code, please try again!';
}
0 Comment(s)