javascript reCAPTCHA PHP 和 AJAX
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14482516/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
reCAPTCHA PHP and AJAX
提问by Rick
I am trying to add a reCAPTCHA to my PHP contact form with AJAX. I want the user to be displayed with the reCAPTCHA and have the user enter it. If it is incorrect it should display so without leaving the page. Only if the CAPTCHA is correct should it go to contacts-go.php
and run the code to send the e-mail. I tried to do it from thisexample.
我正在尝试使用 AJAX 将 reCAPTCHA 添加到我的 PHP 联系表单中。我希望用户与 reCAPTCHA 一起显示并让用户输入它。如果不正确,它应该在不离开页面的情况下显示。只有在验证码正确的情况下,它才能contacts-go.php
运行代码来发送电子邮件。我试图从这个例子中做到这一点。
This is what I have on my contact.phppage:
这是我在contact.php页面上的内容:
<?php
session_start();
?>
<html>
<head>
<script type="text/javascript" src="/js/jquery-1.4.2.min.js"></script>
<SCRIPT type="text/javascript">
function validateCaptcha()
{
challengeField = $("input#recaptcha_challenge_field").val();
responseField = $("input#recaptcha_response_field").val();
//alert(challengeField);
//alert(responseField);
//return false;
var html = $.ajax({
type: "POST",
url: "ajax.recaptcha.php",
data: "recaptcha_challenge_field=" + challengeField + "&recaptcha_response_field=" + responseField,
async: false
}).responseText;
if(html == "success")
{
$("#captchaStatus").html(" ");
// Uncomment the following line in your application
return true;
}
else
{
$("#captchaStatus").html("Your captcha is incorrect. Please try again");
Recaptcha.reload();
return false;
}
}
</SCRIPT>
<script type="text/javascript">
</script>
</head>
<body>
<?php
require_once("ajax.recaptcha.php");
?>
<div id="login">
<form method="post" onSubmit="return validateCaptcha()" action="contacts-go.php">
<label>Message</label>
<br />
<textarea type="text" id="name" name="message" size="20"></textarea>
<p><?php echo recaptcha_get_html($publickey);?></p>
<p style="color: red;" id="captchaStatus"> </p>
<input type=submit name="submit" value="Submit">
</form>
</div>
</body>
</html>
This is the ajax.recaptcha.phpfile
这是ajax.recaptcha.php文件
<?php
//A. Load the Recaptcha Libary
require_once("recaptchalib.php");
require_once("recaptha-keys.php"); // my reCAPTCHA Keys
$resp = recaptcha_check_answer ($privatekey,
$_SERVER["REMOTE_ADDR"],
$_POST["recaptcha_challenge_field"],
$_POST["recaptcha_response_field"]);
if ($resp->is_valid) {
?>success<?
$_SESSION['captcha'] = 1;
}
else
{
die ("The reCAPTCHA wasn't entered correctly. Go back and try it again." .
"(reCAPTCHA said: " . $resp->error . ")");
}
?>
All I am getting is a blank page with the following:
我得到的只是一个空白页,内容如下:
The reCAPTCHA wasn't entered correctly. Go back and try it again.(reCAPTCHA said: incorrect-captcha-sol)
The reCAPTCHA wasn't entered correctly. Go back and try it again.(reCAPTCHA said: incorrect-captcha-sol)
回答by OMG
I saw the contents of recaptchalib.php
Noticed that, the api url and verify url doesnot work.
define("RECAPTCHA_API_SERVER", "http://api.recaptcha.net"); define("RECAPTCHA_API_SECURE_SERVER", "https://api-secure.recaptcha.net"); define("RECAPTCHA_VERIFY_SERVER", "api-verify.recaptcha.net");
Use this tutorial: http://www.jquery4u.com/forms/setup-user-friendly-captcha-jqueryphp/
It uses google recaptcha.
More links:
我看到了recaptchalib.php的内容
请注意,api url 和 verify url 不起作用。
define("RECAPTCHA_API_SERVER", "http://api.recaptcha.net"); define("RECAPTCHA_API_SECURE_SERVER", "https://api-secure.recaptcha.net"); define("RECAPTCHA_VERIFY_SERVER", "api-verify.recaptcha.net");
使用本教程:http: //www.jquery4u.com/forms/setup-user-friendly-captcha-jqueryphp/
它使用谷歌验证码。
更多链接: