1. Modify the two methods in recaptchalib.php
Copy Code code as follows:
function _recaptcha_http_post ($host, $path, $data, $port = 80) {
$req = _recaptcha_qsencode ($data);
$response = ';
$url = $host. $path;
$post _data = $req;
$ch = Curl_init ();
curl_setopt ($ch, Curlopt_url, $url);
curl_setopt ($ch, Curlopt_returntransfer, 1);
We are in post data Oh!
curl_setopt ($ch, Curlopt_post, 1);
Add a post variable to the
curl_setopt ($ch, Curlopt_postfields, $post _data);
$output = curl_exec ($ch);
Curl_close ($ch);
Echo $output;
$response = $output;
return $response;
}
function Recaptcha_check_answer ($privkey, $remoteip, $challenge, $response, $extra _params = Array ())
{
if ($privkey = = NULL | | $privkey = = ") {
Die ("To use reCAPTCHA your must get a API key from <a href= ' https://www.google.com/recaptcha/admin/create ' >https:// Www.google.com/recaptcha/admin/create</a> ");
}
if ($remoteip = = NULL | | $remoteip = = ") {
Die ("For security reasons, your must pass the remote IP to reCAPTCHA");
}
Discard spam Submissions
if ($challenge = null | | strlen ($challenge) = = 0 | | $response = NULL | | strlen ($response) = = 0) {
$recaptcha _response = new Recaptcharesponse ();
$recaptcha _response->is_valid = false;
$recaptcha _response->error = ' Incorrect-captcha-sol ';
return $recaptcha _response;
}
$response = _recaptcha_http_post (Recaptcha_verify_server, "/recaptcha/api/verify",
Array (
' Privatekey ' => $privkey,
' Remoteip ' => $remoteip,
' Challenge ' => $challenge,
' Response ' => $response
) + $extra _params
);
$answers = explode ("\ n", $response [1]);
$recaptcha _response = new Recaptcharesponse ();
$pos = Strpos ($response, ' true ');
if ($pos = = False) {
$recaptcha _response->is_valid = false;
$recaptcha _response->error = $response;
} else {
$recaptcha _response->is_valid = true;
}
return $recaptcha _response;
}
2.demo.php
Copy Code code as follows:
<body>
<form action= "" method= "POST" >
<?php
Require_once (' recaptchalib.php ');
Get a key from Https://www.google.com/recaptcha/admin/create
$publickey = "Your public key---yourself to http://www.google.com/recaptcha application";
$privatekey = "Your private key---yourself to http://www.google.com/recaptcha application";
# 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 we can display it
$error = $resp->error;
Echo $error;
echo $_post["Recaptcha_challenge_field"];
echo $_post["Recaptcha_response_field"];
}
}
Echo recaptcha_get_html ($publickey, $error);
?>
<br/>
<input type= "Submit" value= "Submit"/>
</form>
</body>