Last active
February 20, 2017 17:45
-
-
Save madan712/4972644 to your computer and use it in GitHub Desktop.
PHP example - Google Recaptcha validation using jquery (AJAX)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Зачем вверху выводить ip адрес, к чему это вообще там, ему место в validateRecaptcha.php! Удачи!