Forum Moderators: coopster

Message Too Old, No Replies

Google ReCaptcha V2. Never passes/success

         

tonynoriega

6:13 pm on May 16, 2016 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Sorry all.. wasnt sure if this goes here in PHP or somewhere in Google. I found a few others in PHP, so am giving it a shot.

I have a site that originally used Google reCaptcha v1, and worked successfully. Thought I would try V2. Please keep in mind my PHP and general scripting skills are not top notch. Maybe not even middle notch, anywho.

What I am noticing is that if the reCaptcha is left blank, the proper message is displayed on the Thank You page. Which isnt ideal, but still functions properly. However, when it is checked, and the images are properly selected by the user, no matter what I get failure.

I dont know if there is something askew in my IF ELSE statements, or I am missing something, but I cannot figure this out, as to why it never passes.

I have the script in the <head>
<script src='https://www.google.com/recaptcha/api.js'></script>


Then my form (simplified).

 <form action="../thank-you2.php" method="post" name="contact_form" onsubmit="return validate_form()" id="contact_form" >
<div>
<div >
<input name="name" placeholder="Name" type="text" />
</div>
<div>
<input name="email" placeholder="Email" type="text" />
</div>
</div>

<div>
<div>
<div class="g-recaptcha" data-sitekey="(site key here)"></div>
</div>
</div>


<div>
<div >

<label for="submit"></label>
<input type="submit" value="Submit" class="submit" name="submit" />
</div>
</div>
</form>


Then my thank you page for processing:
 <?php if(isset($_POST['submit'])) {

$to = "me@example.com";
$subject = "Free Estimate Contact Form";
$name = $_POST['name'];
$email = $_POST['email'];
$captcha = $_POST['g-recaptcha-response'];

if(!$captcha){
echo '<h2>Please check the the captcha form.</h2>';
exit;
}
$secretKey = "(secret key here)";
$ip = $_SERVER['REMOTE_ADDR'];
$response=file_get_contents("https://www.google.com/recaptcha/api/siteverify?secret=".$secretKey."&response=".$captcha."&remoteip=".$ip);
$responseKeys = json_decode($response,true);

if(intval($responseKeys["success"]) !== 1) {
echo '<h2>You are spammer ! Get the @$%K out</h2>';
} else {
$body = "Name: $name\n
E-Mail: $email\n
echo "<h2>Your request has been submitted</h2>";
$from = "From: contact@example.com";
mail($to, $subject, $body, $from);
}

}
?>

coopster

2:45 pm on May 25, 2016 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



I'm not entirely sure if/what is working or not working properly, but this appears to be a syntax error to me as you begin to echo in the middle of a string variable:
$body = "Name: $name\n
E-Mail: $email\n
echo "<h2>Your request has been submitted</h2>";