我无法将Recaptcha嵌入电子邮件表单中

时间:2014-10-26 12:12:00

标签: php html email recaptcha

我正在尝试将recaptcha实现到我正在处理的网站上的电子邮件表单中,但它无法正常工作,我无法弄清楚为什么...我删除了所有的重新访问代码并且表单有效好吧,我想我的错误在于某处的重新处理,但我不确定。任何帮助将不胜感激

这是表格

<!-- start contact form -->
                <form method="POST" name="contact_form" action="contact-form-handler.php">
                    <h4>Your Name:</h4>
                    <input type="text" name="name" size="35">
                    <h4>Email Address:</h4>
                    <input type="text" name="email" size="35">
                    <h4>Message:</h4>
                    <textarea name="message" rows="6" cols="37"></textarea>
                    <div>
                        <script type="text/javascript" 
                                src="http://www.google.com/recaptcha/api/challenge?k=publickey">
                        </script>
                        <noscript>
                        <iframe src="http://www.google.com/recaptcha/api/noscript?k=publickey"
                                height="300" width="500" frameborder="0"></iframe><br>
                        <textarea name="recaptcha_challenge_field" rows="3" cols="40">
                        </textarea>
                        <input type="hidden" name="recaptcha_response_field"
                            value="manual_challenge">
                        </noscript>
                    </div>
                    <div>
                        <input type="submit" value="Submit">
                    </div>

                    <script language="JavaScript">
                    var frmvalidator  = new Validator("contact_form");
                    frmvalidator.addValidation("name","req","Please provide your name");
                    frmvalidator.addValidation("email","req","Please provide your email");
                    frmvalidator.addValidation("email","email","Please enter a valid email address");
                    frmvalidator.addValidation("message","req","Please write a message");   
                    frmvalidator.addValidation("recaptcha_challenge_field", "req", "Please answer the captcha test");
                    </script>
                </form>
                <!-- end contact form-->

这是处理它的后端php ...

    <?php
require_once('/scripts/recaptcha/recaptchalib.php');
$privatekey = "PRIVATE KEY";
$resp = recaptcha_check_answer ($privatekey, $_SERVER["REMOTE_ADDR"], $_POST["recaptcha_challenge_field"], $_POST["recaptcha_response_field"]);

if (!$resp->is_valid) {
header('Location: contact.html')
exit;

} else {

$myemail = 'myemail';

$to = $myemail;

$email_subject = "Contact form submission: $name";

$email_body = "You have received a new message. ".

" Here are the details:\n Name: $name \n ".

"Email: $email_address\n Message \n $message";

$headers = "From: $myemail\n";

$headers .= "Reply-To: $email_address";

$email_body = wordwrap($email_body, 70);

mail($to,$email_subject,$email_body,$headers);

header('Location: contact_thanks.html');

exit;

}
?>

1 个答案:

答案 0 :(得分:0)

您必须在此处插入recaptcha提供的密钥:src="http://www.google.com/recaptcha/api/challenge?k=publickey"和此处src="http://www.google.com/recaptcha/api/noscript?k=publickey"(将“publickey”替换为recaptcha提供的公钥)

将recaptcha给出的私钥放在php文件中:$privatekey = "PRIVATE KEY";

如果您仍有问题,有时候recaptcha会生成无效密钥,如果您不方便破坏密钥,则必须等待一天左右才能解决问题。

如果您还没有完成此操作,请注册here

根据我自己的经验非常重要的通知,如果你确实得到了破损的钥匙,那么recaptcha根本就不会出现,你将不会得到任何错误!我不得不手动访问链接,看到重新访问密钥将返回500内部服务器错误,我不得不等待他们修复这些密钥。