admin管理员组文章数量:1426217
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 this example.
This is what I have on my contact.php page:
<?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(" ");
// Unment 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.php file
<?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)
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 this example.
This is what I have on my contact.php page:
<?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(" ");
// Unment 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.php file
<?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)
-
Removing the AJAX validateCaptcha function and changing to
action="ajax.recaptcha.php
, I notice that it seems to get get a "success". Aperently I have something wrong in my AJAX function that keeps getting false on$resp->is_valid
– Rick Commented Jan 23, 2013 at 17:49
1 Answer
Reset to default 2I saw the contents of recaptchalib.php
Noticed that, the api url and verify url doesnot work.
define("RECAPTCHA_API_SERVER", "http://api.recaptcha"); define("RECAPTCHA_API_SECURE_SERVER", "https://api-secure.recaptcha"); define("RECAPTCHA_VERIFY_SERVER", "api-verify.recaptcha");
Use this tutorial: http://www.jquery4u./forms/setup-user-friendly-captcha-jqueryphp/
It uses google recaptcha.
More links:
http://www.google./recaptcha/learnmore
https://developers.google./recaptcha/docs/php
本文标签: javascriptreCAPTCHA PHP and AJAXStack Overflow
版权声明:本文标题:javascript - reCAPTCHA PHP and AJAX - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745381464a2656181.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论