PHP space does not support sockets but reCAPTCHA use _php techniques when supporting curl

Source: Internet
Author: User
Tags curl strlen
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>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.