PHP jQuery:使用AJAX验证Recaptcha'

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了PHP jQuery:使用AJAX验证Recaptcha'相关的知识,希望对你有一定的参考价值。

<script type="text/javascript" charset="utf-8" src="/location/to/jquery.1.3.1.min.js"></script>
<script type="text/javascript" charset="utf-8">
//Validate the Recaptcha' Before continuing with POST ACTION
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: "/location/to/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", "http://action/to/the/form_handler");
		//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;
	}
}	
</script>

<?php
/*
**    @Title: Recaptcha Validation
**    @Date Modified: 6/01/09
**    Instructions: This code lives within the form.  Generally above the <SUBMIT> button
*/

//A.This div notifies the user whether the Recaptcha was Successful or not
echo "\n\t" . '<div id="captchaStatus" style="color:red;font-size:12px"></div>';

//B. This script provides Recaptcha with a Theme
echo "\n\t" .'<script type="text/javascript" charset="utf-8">';
echo "\n\t" .'var RecaptchaOptions = { theme: "clean" };';
echo "\n\t" . '</script>' . "\n";

//C. Require the Recaptcha Library
require_once('/location/to/recaptchalib.php');

//D. READ ME!
$publickey = "RECAPTCHA_PUBLIC_KEY"
recaptcha_get_html( $publickey );
?>


<?php
/*
**    @Title: Recaptcha Validation
**    @Date Modified: 6/01/09
**    Instructions: Place this code in a file called "ajax.recaptcha.php"
*/

//A. Load the Recaptcha Libary
require_once('/location/to/recaptchalib.php');

$privatekey = "RECAPTCHA_PRIVATE_KEY";

//B. Recaptcha Looks for the POST to confirm 
$resp = recaptcha_check_answer ($privatekey,
                                $_SERVER["REMOTE_ADDR"],
                                $_POST["recaptcha_challenge_field"],
                                $_POST["recaptcha_response_field"]);

//C. If if the User's authentication is valid, echo "success" to the Ajax
if ($resp->is_valid) {
	echo "success";
} else {
    die ("The reCAPTCHA wasn't entered correctly. Go back and try it again." .
       "(reCAPTCHA said: " . $resp->error . ")");
}
?>

以上是关于PHP jQuery:使用AJAX验证Recaptcha'的主要内容,如果未能解决你的问题,请参考以下文章

使用jQuery/AJAX/PHP轻松设置验证码

使用带有摘要身份验证、jquery ajax 调用的 RestFul PHP Web 服务

AJAX 实时电子邮件验证 (PHP)

是否可以使用 jQuery 加密 AJAX 调用以进行身份​​验证?

使用 Jquery 验证插件提交 Ajax 表单不起作用

PHP、MySQL、jQuery、AJAX:json 数据返回正确响应但前端返回错误