欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

php空间不支持socket但支持curl时recaptcha的用法

程序员文章站 2023-10-17 16:19:54
1.修改recaptchalib.php中的两个方法 复制代码 代码如下: function _recaptcha_http_post($host, $path, $dat...
1.修改recaptchalib.php中的两个方法
复制代码 代码如下:

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);
// 我们在post数据哦!
curl_setopt($ch, curlopt_post, 1);
// 把post的变量加上
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 <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, 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
复制代码 代码如下:

<html>
<body>
<form action="" method="post">
<?php
require_once('recaptchalib.php');
// get a key from https://www.google.com/recaptcha/admin/create
$publickey = "你的公共key ---自己去http://www.google.com/recaptcha申请";
$privatekey = "你的私有key ---自己去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);
?>
<br/>
<input type="submit" value="submit" />
</form>
</body>
</html>