1) 从这里下载 recaptcha 库https://code.google.com/p/recaptcha/downloads/detail?name=recaptcha-php-1.11.zip&can=2&q=label%3Aphplib-Latest
2) 解压并将文件复制recaptchalib.php
到表单文件所在的目录中。
3)在这里注册recaptcha http://www.google.com/recaptcha/whyrecaptcha
4)复制公钥并将其粘贴到您的表单文件中的正确位置,该文件必须如下所示:
<html>
<body> <!-- the body tag is required or the CAPTCHA may not show on some browsers -->
<!-- your HTML content -->
<form method="post" action="verify.php">
Name:<input type="text" name="name"> <br>
Email: <input type="email" name="email">
<?php
require_once('recaptchalib.php');
$publickey = "your public key here"; // you got this from the signup page
echo recaptcha_get_html($publickey);
?>
<input type="submit" />
</form>
<!-- more of your HTML content -->
</body>
</html>
5) 然后创建另一个文件 verify.php,它也必须与您的表单和 recaptchalib.php 文件一起存在。
<?php
require_once('recaptchalib.php');
$privatekey = "your private key here";
$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
echo "Success!";
}
?>
6)从您获得公钥的页面中,现在复制私钥并将其粘贴到verify.php
文件中的正确位置。