大家好,我在我的页面上的联系人页面使用验证码。我想在PHP提交表单前检查的话。所以,我在阿贾克斯做到了。这里是我的code。
< DOCTYPE HTML PUBLIC - // W3C // DTD XHTML 1.0过渡// ENHTTP://www.w3/TR/xhtml1/DTD/xhtml1- transitional.dtd>
< HTML的xmlns =http://www.w3.org/1999/xhtml>
< HEAD>
< META HTTP-当量=Content-Type的CONTENT =text / html的;字符集= UTF-8/>
<冠军>再次验证码< /标题>
<脚本类型=文/ JavaScript的SRC =jQuery的-1.2.3.js>< / SCRIPT>
<脚本类型=文/ JavaScript的>
功能validateCaptcha()
{
challengeField = $(#输入recaptcha_challenge_field)VAL()。
responseField = $(#输入recaptcha_response_field)VAL()。
警报(challengeField);
警报(responseField);
//返回false;
VAR HTML = $阿贾克斯({
键入:POST,
网址:ajax.recaptcha.php
数据:recaptcha_challenge_field =+ challengeField +与& recaptcha_response_field =+ responseField,
异步:假的
})responseText的;
如果(HTML ==成功)
{
// $(#captchaStatus)HTML(成功提交表单。)。
//返回false;
//取消注释在应用程序中以下行
// load_ajax();
警报(验证码成功。);
返回true;
}
其他
{
$(#captchaStatus)HTML(图片验证失败);
Recaptcha.reload();
返回false;
}
}
< / SCRIPT>
< / SCRIPT>
< /头>
<身体GT;
< DIV的风格=HEIGHT:300px的;宽度:350像素;保证金:汽车;背景:#969;填充:20像素;>
<形式的行动=JavaScript的:无效(NULL); NAME =frmSubmit方法=邮报的onsubmit =返回validateCaptcha();>
<标签>名称和LT; /标签>< BR />
<输入类型=文本名称=改为txtNameID =NameTextBox/>
< BR />
<标签> E邮< /标签>< BR />
<输入类型=文本名称=txtEmailID =EmailTextBox/>
< BR />
< BR />
< PHP
require_once('验证码/ recaptchalib.php');
//从https://www.google.com/recaptcha/admin/create的关键
//本地主机
$公钥=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx;
$ privatekey =xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx;
#从验证码的响应
$ RESP = NULL;
#错误$ C $的验证码,C如有
$错误= NULL;
#是有验证码的反应?
如果($ _ POST [recaptcha_response_field]){
$ RESP = recaptcha_check_answer($ privatekey,
$ _ SERVER [REMOTE_ADDR],
$ _ POST [recaptcha_challenge_field],
$ _ POST [recaptcha_response_field]);
如果($ resp-> is_valid){
回声你猜对了!
} 其他 {
#设置错误code,这样我们就可以显示它
$错误= $ resp->错误;
}
}
回声recaptcha_get_html($公钥,$错误);
?>
< BR />
<输入name="BtnSubmit"type="submit"onclick="MM_validateForm('NameTextBox','','R','EmailTextBox','','R');return document.MM_returnValue值=发送/>
< /形式GT;
< / DIV>
<脚本类型=文/ JavaScript的>
VAR RecaptchaOptions = {
主题:红色
};
< / SCRIPT>
< /身体GT;
< / HTML>
我得到的错误是提起chlenge(challengeField = $(#输入recaptcha_challenge_field)VAL();)我得到不正确。它看起来像这样
03AHJ_Vut2oHufxF2RvOtWbL7PDrlbU0bj3OgVpxaRg0Ae6-_xjqcIxRJTqD-bnUdTwyeLepickvVC7XLe5kqoWLu1sYdx7ZlmVkEP5TrHA9jGH75kW0Wua6faimq4aRZ5RaOOI0KOoqD0gVOGfeRQ1fw5c5lw-l5WXQ解决方案
绑定在尝试在你的PHP网站上使用此验证码为整合
只需使用 Zend_Service_ReCaptcha 。你会融入这项服务只需用几行:
//创建实例
$验证码=新Zend_Service_ReCaptcha($ PUBKEY,$的privKey);
//显示输出
回声$ recaptcha-> getHTML();
//处理输入
$结果= $ recaptcha->验证(
$ _ POST ['recaptcha_challenge_field'],
$ _ POST ['recaptcha_response_field']
);
//最后验证验证码
如果($ result->的isValid()){
//凉!
}
Hi all I am using reCaptcha in my page its a contact page. I want to check the words before submitting the form in PHP. So i done it in Ajax. here is my code.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Re captcha</title>
<script type="text/javascript" src="jquery-1.2.3.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("Success. Submitting form.");
// return false;
// Uncomment the following line in your application
//load_ajax();
alert("Captcha success..");
return true;
}
else
{
$("#captchaStatus").html("Image verification failed);
Recaptcha.reload();
return false;
}
}
</script>
</script>
</head>
<body>
<div style="height:300px;width:350px;margin:auto;background:#969;padding:20px;>
<form action = "javascript:void(null);" name="frmSubmit" method="post" onsubmit="return validateCaptcha();">
<label>Name</label><br />
<input type="text" name="txtName" id="NameTextBox" />
<br />
<label>E Mail</label><br />
<input type="text" name="txtEmail" id="EmailTextBox" />
<br />
<br />
<?php
require_once('captcha/recaptchalib.php');
// Get a key from https://www.google.com/recaptcha/admin/create
//Localhost
$publickey = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
$privatekey = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
# the response from reCAPTCHA
$resp = null;
# the error code from reCAPTCHA, if any
$error = null;
# was there a reCAPTCHA response?
if ($_POST["recaptcha_response_field"]) {
$resp = recaptcha_check_answer ($privatekey,
$_SERVER["REMOTE_ADDR"],
$_POST["recaptcha_challenge_field"],
$_POST["recaptcha_response_field"]);
if ($resp->is_valid) {
echo "You got it!";
} else {
# set the error code so that we can display it
$error = $resp->error;
}
}
echo recaptcha_get_html($publickey, $error);
?>
<br />
<input name="BtnSubmit"type="submit"onclick="MM_validateForm('NameTextBox','','R','EmailTextBox','','R');return document.MM_returnValue" value="Send" />
</form>
</div>
<script type="text/javascript">
var RecaptchaOptions = {
theme : 'Red'
};
</script>
</body>
</html>
The error I am getting is the chlenge filed(challengeField = $("input#recaptcha_challenge_field").val();) i am getting is incorrect. It looking like this
03AHJ_Vut2oHufxF2RvOtWbL7PDrlbU0bj3OgVpxaRg0Ae6-_xjqcIxRJTqD-bnUdTwyeLepickvVC7XLe5kqoWLu1sYdx7ZlmVkEP5TrHA9jGH75kW0Wua6faimq4aRZ5RaOOI0KOoqD0gVOGfeRQ1fw5c5lw-l5WXQ
解决方案
Bound over try to use this for recaptcha integration on your PHP site
Just use Zend_Service_ReCaptcha. You'll integrate this service just with few lines:
//Creating instance
$recaptcha = new Zend_Service_ReCaptcha($pubKey, $privKey);
//Display output
echo $recaptcha->getHTML();
//Handling input
$result = $recaptcha->verify(
$_POST['recaptcha_challenge_field'],
$_POST['recaptcha_response_field']
);
//And finally validate captcha
if ($result->isValid()) {
//Cool!
}