- client side key
- Server side key
https://www.google.com/recaptcha/admin/create | https://www.google.com/recaptcha/admin/
Load the JavaScript API with your client-side key.
<script async src="https://www.google.com/recaptcha/api.js?render=(client side key)></script>
Box code to confirm, paste it in the desired location.
<div class="g-recaptcha" data-sitekey="(client side key)"></div>
Add an attribute to your button.
<button class="g-recaptcha" data-sitekey="(client side key)" data-callback="onSubmit" data-action="submit">Submit</button>
Server side code (php) / (javascript)
// php
<?php
define("secretkey", "Server side key");
$result = recaptcha();
if ($result["success"]) {
// Added what to do when verified correctly
} else {
// Added what to do when incorrect identity verification
}
function recaptcha() {
$options = [
"secret" => secretkey,
"response" => filter_input(INPUT_POST, "g-recaptcha-response"),
"remoteip" => $_SERVER["REMOTE_ADDR"]
];
$url = "https://www.google.com/recaptcha/api/siteverify?" . http_build_query($options);
$result = json_decode(file_get_contents($url), true);
return $result;
}
?>
// javascript
(() => {
grecaptcha.ready(function () {
grecaptcha.execute("Server side key", { action: "submit" }).then(function (token) {
console.log(token);
});
});
})();
๐ Thank Ref : reCAPTCHA v2 | reCAPTCHA v3