Reputation: 167
EDIT: I'm using reCaptcha 2.0, should I not be using recaptchalib.php?
Live DEMO http://josiahbertoli.com/
I'm getting the following error when I submit a form (ignore the blank values for the input fields)(with debug code):
Error
_POST: =========
Array
(
[first_name] =>
[last_name] =>
[email] =>
[subject] =>
[comments] =>
[g-recaptcha-response] => big-long-value
)
=========
The reCAPTCHA wasn't entered correctly. Go back and try it again.(reCAPTCHA said: )`
How my webpage is set up:
HTML
<form id="query-form" action="wp-content/themes/portfolio/submit-form.php" method="post" name="myForm">
<input id="first_name" name="first_name" size="35" type="text" placeholder="e.g. John" />
<input id="last_name" name="last_name" size="35" type="text" placeholder="e.g. Smith" />
<input id="email" name="email" size="35" type="text" placeholder="e.g. [email protected]" />
<input id="subject" name="subject" size="35" type="text" placeholder="e.g. Feedback" />
<textarea id="comments" name="comments"></textarea>
<div class="g-recaptcha" data-sitekey="6Le8WxcTAAAAAGqymotU9wtOBFEmWgjM3j2kqTcB"></div>
<input type="submit" value="Submit" />
</form>
Submit uses method POST to call the action submit-form.php which is as follows
PHP
<?php
require_once('recaptchalib.php');
$privatekey = "the-key-that-i-put-in";
echo "<pre> _POST: =========\n"; print_r($_POST); echo "\n=========\n</pre>";
$resp = recaptcha_check_answer ($privatekey, $_SERVER["REMOTE_ADDR"],
$_POST["recaptcha_challenge_field"],
$_POST["recaptcha_response_field"]);
$resp = null;
if (!$resp->is_valid) {
// What happens when the CAPTCHA was entered incorrectly
die ("The reCAPTCHA wasn't entered correctly. Go back and try it again." . "(reCAPTCHA said: " . $resp->error . ")"); }
else {
// Your code here to handle a successful verification
include '../../../../process.php';
}
?>
Thank you for your responses, I appreciate it.
Upvotes: 2
Views: 8872
Reputation: 16963
For displaying the reCAPTCHA widget you don't need any library file, you just have to include the necessary JavaScript resource, like this:
<html>
<head>
<title>reCAPTCHA demo: Simple page</title>
<script src="https://www.google.com/recaptcha/api.js" async defer></script>
</head>
<body>
<form action="?" method="POST">
<div class="g-recaptcha" data-sitekey="your_site_key"></div>
<br/>
<input type="submit" value="Submit">
</form>
</body>
</html>
Here's the reference:
Now comes to your user's response. Since you're using Google reCAPTCHA V2, you should fetch the user's response using the POST parameter g-recaptcha-response.
Here's the reference:
So your code should be like this:
<?php
if(isset($_POST['g-recaptcha-response']) && !empty($_POST['g-recaptcha-response'])){
$privatekey = "YOUR_PRIVATE_KEY";
//get verified response data
$param = "https://www.google.com/recaptcha/api/siteverify?secret=".$privatekey."&response=".$_POST['g-recaptcha-response'];
$verifyResponse = file_get_contents($param);
$responseData = json_decode($verifyResponse);
if($responseData->success){
// success
echo "success";
}else{
// failure
echo "failure";
}
}else{
// user didn't enter reCAPTCHA
echo "The reCAPTCHA wasn't entered correctly. Go back and try it again.";
}
?>
Upvotes: 3