I'm trying to make a good registration form. At first I made a PHP validation script, because I know for sure that it is secure. When I posted my form incorrectly and got an error returned, all my input values were gone. I didn't like that so I learned about validation with Javascript. I don't have much experience with Javascript, but I've managed to validate my form with both client-side and server-side validation, except for the ReCaptcha field.
The problem is that if I fill in the code correctly and press Submit, the client-side verification returns 'Succes' but the server-side validation always returns 'Wrong'.
If I only validate with the server-side script it will return 'Succes' though, so the script itself is fine but somehow they won't work after each other with the same code.
Client-side recaptcha validation script:
function validateCaptcha()
{
challengeField = $("input#recaptcha_challenge_field").val();
responseField = $("input#recaptcha_response_field").val();
//console.log(challengeField);
//console.log(responseField);
//return false;
var html = $.ajax({
type: "POST",
url: "handlers/ajax.recaptcha.php",
data: "recaptcha_challenge_field=" + challengeField + "&recaptcha_response_field=" + responseField,
async: false
}).responseText;
//console.log( html );
if(html == "success") {
//Add the Action to the Form
$("form").attr("action", "handlers/register_handler.php");
//Indicate a Successful Captcha
$("#captchaStatus").html("Success!");
// Uncomment the following line in your application
return true;
} else {
$("#captchaStatus").html("The security code you entered did not match. Please try again.");
Recaptcha.reload();
return false;
}
}
ajax.recaptcha.php
<?php
require_once('recaptchalib.php');
$privatekey = "private key";
$resp = recaptcha_check_answer ($privatekey,
$_SERVER["REMOTE_ADDR"],
$_POST["recaptcha_challenge_field"],
$_POST["recaptcha_response_field"]);
if ($resp->is_valid) {
echo "success";
} else {
die ("The reCAPTCHA wasn't entered correctly. Go back and try it again." .
"(reCAPTCHA said: " . $resp->error . ")");
}
?>
Server-side recaptcha validation script:
require_once('recaptchalib.php');
$privatekey = "private key";
$resp = recaptcha_check_answer ($privatekey,
$_SERVER["REMOTE_ADDR"],
$_POST["recaptcha_challenge_field"],
$_POST["recaptcha_response_field"]);
if (!$resp->is_valid) {
echo "error"; }
else { echo "succes"; }
They're both allmost the same, so I really don't know what I'm doing wrong here.
Please help me out because I don't want to enter all the information again when I've only got the Captcha code wrong.
Thanks in advance
EDIT: I've discovered the fact that the same challenge field code is used for both client-side verification as the server-side verification. Maybe it won't work because the challenge field code can only be used once? If so, is there a way to make this work for me?