Google recaptcha using php

Google recaptcha using php

https://www.google.com/recaptcha/admin/create
site_key:  6LcvdScrAAAAAPcLNrU6Yn4iSHv9bUatTi600pab
secret_key:  6LcvdScrAAAAABW238OshnA0j46EgUd9sVwvqqs4
<script src="https://www.google.com/recaptcha/api.js" async defer></script>

<form action="submit.php" method="post">
<!-- Your form fields here -->
<div class="g-recaptcha" data-sitekey="YOUR_SITE_KEY"></div>
<input type="submit" value="Submit">
</form>
submit.php

<?php
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$secretKey = 'YOUR_SECRET_KEY';
$responseKey = $_POST['g-recaptcha-response'];
$userIP = $_SERVER['REMOTE_ADDR'];

// Make a POST request to the Google reCAPTCHA API
$url = 'https://www.google.com/recaptcha/api/siteverify';
$data = [
'secret' => $secretKey,
'response' => $responseKey,
'remoteip' => $userIP
];

$options = [
'http' => [
'header' => "Content-type: application/x-www-form-urlencoded\r\n",
'method' => 'POST',
'content' => http_build_query($data),
],
];
$context = stream_context_create($options);
$verify = file_get_contents($url, false, $context);
$captchaSuccess = json_decode($verify);

if ($captchaSuccess->success) {
// Proceed with form processing
echo "Verification successful.";
} else {
// Handle verification failure
echo "Verification failed. Please try again.";
}
}
?>



 

 

Leave a Reply