Skip to content

Instantly share code, notes, and snippets.

@madan712
Last active February 20, 2017 17:45
Show Gist options
  • Save madan712/4972644 to your computer and use it in GitHub Desktop.
Save madan712/4972644 to your computer and use it in GitHub Desktop.
PHP example - Google Recaptcha validation using jquery (AJAX)
<?php
$remoteip = $_SERVER['REMOTE_ADDR'];
?>
<!doctype html>
<html>
<head>
<title> Recaptcha validation using jquery ajax </title>
<script src="http://code.jquery.com/jquery-1.9.1.js"></script>
<script type="text/javascript" src="recaptcha_ajax.js"></script>
<script type="text/javascript">
function reloadRecaptcha() {
var publicKey = "your_public_key";
var div = "recap";
Recaptcha.create(publicKey,div,{theme: "white"});
return false;
}
function validate() {
var challenge = Recaptcha.get_challenge();
var response = Recaptcha.get_response();
var remoteip = "<?php echo $remoteip; ?>";
$.ajax({
type: "POST",
url: "validateRecaptcha.php",
async: false,
data: {
remoteip: remoteip,
challenge: challenge,
response: response
},
success: function(resp) {
if(resp == "true") {
document.getElementById("message").innerHTML = "Perfect!";
}
else {
document.getElementById("message").innerHTML = "Incorrect Recaptcha! Please try again!";
reloadRecaptcha();
}
}
});
return false;
}
</script>
</head>
<body onload="return reloadRecaptcha();">
<form method="post" onsubmit="return validate();">
<table>
<tr>
<td colspan="2">
<div id="message" style="color:#ff0000; "></div>
<div id="recap"></div>
</td>
</tr>
<tr>
<td colspan="2" align="center">
<input type="submit" value="Submit">
</td>
</tr>
</table>
</form>
</body>
</html>
@the-loker
Copy link

Зачем вверху выводить ip адрес, к чему это вообще там, ему место в validateRecaptcha.php! Удачи!

@sathishvijay
Copy link

no related files to download

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment