Check out this page, which is where this is shamelessly taken from:
从您的代码示例中,您似乎正在尝试在<iframe> 中生成recaptcaha。这不会很容易 - 验证码的目的是让您提交一个带有附加值的表单,您的代码应该验证服务器端。
只需起草一个普通的 HTML 表单(去掉 iframe 部分),然后删除创建 recaptacha 部分的 php 代码,就像在表单 (contact_form.php) 中一样:
<form method="post" action="verify.php">
<?php
require_once('recaptchalib.php');
$publickey = "your_public_key"; // you got this from the signup page
echo recaptcha_get_html($publickey);
?>
<input type="submit" />
</form>
当页面被渲染时,php 被替换为 javascript。当用户提交您的表单时,它会被发布到上面action 中的 url,这是验证 capatcha 输入 (verify.php) 的服务器端代码:
<?php
require_once('recaptchalib.php');
$privatekey = "your_private_key";
$resp = recaptcha_check_answer ($privatekey,
$_SERVER["REMOTE_ADDR"],
$_POST["recaptcha_challenge_field"],
$_POST["recaptcha_response_field"]);
if (!$resp->is_valid) {
// What happens when the CAPTCHA was entered incorrectly
die ("The reCAPTCHA wasn't entered correctly. Go back and try it again." .
"(reCAPTCHA said: " . $resp->error . ")");
} else {
// Your code here to handle a successful verification
}
?>
就是这样。