Home > Backend Development > PHP Tutorial > How to use recaptcha when the php space does not support socket but supports curl_PHP tutorial

How to use recaptcha when the php space does not support socket but supports curl_PHP tutorial

WBOY
Release: 2016-07-21 15:22:51
Original
872 people have browsed it

1. Modify the two methods in recaptchalib.php

Copy code The code is 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 POSTing data !
curl_setopt($ch, CURLOPT_POST, 1);
// Add the post variable to
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 you must get an API key from https://www.google.com/recaptcha/admin/create");
}
if ($remoteip == null || $remoteip == '') {
die ("For security reasons, you 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 The code is as follows:




require_once('recaptchalib.php');
// Get a key from https ://www.google.com/recaptcha/admin/create
$publickey = "Your public key --- apply for it yourself at http://www.google.com/recaptcha";
$privatekey = "Your private key --- apply for it yourself at http://www.google.com/recaptcha";
# 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 that 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);
?>






www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/324600.htmlTechArticle1. Modify the two methods in recaptchalib.php and copy the code as follows: function _recaptcha_http_post($host, $path , $data, $port = 80) { $req = _recaptcha_qsencode ($data); $response...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template