I have a contact form and I'm trying to validate google's Recaptcha on the server-side. I'm using reCAPTCHA version 2. My HTML form looks like this:
<div class="container-contact100">
<div class="wrap-contact100">
<form class="contact100-form validate-form" action="form.php" method="POST">
<span class="contact100-form-title">
Contact Us
</span>
<div class="wrap-input100 validate-input" data-validate="Name is required">
<input class="input100" type="text" name="name" placeholder="Full Name">
<span class="focus-input100-1"></span>
<span class="focus-input100-2"></span>
</div>
<div class="wrap-input100 validate-input" data-validate = "Valid email is required: [email protected]">
<input class="input100" type="text" name="email" placeholder="Email">
<span class="focus-input100-1"></span>
<span class="focus-input100-2"></span>
</div>
<div class="wrap-input100 validate-input" data-validate = "Message is required">
<textarea class="input100" name="message" placeholder="Tell us what kind of quote you're looking for"></textarea>
<span class="focus-input100-1"></span>
<span class="focus-input100-2"></span>
</div>
<!-- This is google's test sitekey. -->
<div class="g-recaptcha" data-sitekey="6LeIxAcTAAAAAJcZVRqyHh71UMIEGNQ_MXjiZKhI"></div>
<div class="container-contact100-form-btn">
<button class="contact100-form-btn">
Submit
</button>
</div>
</form>
</div>
</div>
I've already validated this on the client side. But I'm trying to also validate this on the server side. I have PHP code that is attempting to validate it, which is:
<?php
$captcha;
echo '<script>alert("Welcome to Geeks for Geeks")</script>';
if(isset($_POST['g-recaptcha-response'])){
$captcha=$_POST['g-recaptcha-response'];
}
if(!$captcha){
echo '<h2>Please check the the captcha form.</h2>';
exit;
}
$secretKey = "6LeIxAcTAAAAAGG-vFI1TnRWxMZNFuojJ4WifJWe"; // This is google's test key for recaptcha
$ip = $_SERVER['REMOTE_ADDR'];
// post request to server
$url = 'https://www.google.com/recaptcha/api/siteverify?secret=' . urlencode($secretKey) . '&response=' . urlencode($captcha);
$response = file_get_contents($url);
$responseKeys = json_decode($response,true);
// should return JSON with success as true
if($responseKeys["success"]) {
echo '<h2>Thanks for posting comment</h2>';
} else {
echo '<h2>You are spammer ! Get the @$%K out</h2>';
}
?>
However, it doesn't seem to work. Whenever I submit the form, it just goes to a blank page. What am I doing wrong? How can I get it validated?