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

yipeiwu_com5年前PHP代码库
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>

相关文章

PHP自定义错误用法示例

本文实例讲述了PHP自定义错误用法。分享给大家供大家参考,具体如下: 自定义错误就是自己可以完全控制错误以及其提示内容 设定错误由自己定义的函数来处理 set_error_handl...

PHP回调函数与匿名函数实例详解

本文实例讲述了PHP回调函数与匿名函数。分享给大家供大家参考,具体如下: 回调函数和匿名函数 回调函数、闭包在JS中并不陌生,JS使用它可以完成事件机制,进行许多复杂的操作。PHP中却不...

老生常谈PHP位运算的用途

在实际应用中可以做用户权限的应用 我这里说到的权限管理办法是一个普遍采用的方法,主要是使用到”位运行符”操作,& 位与运算符、| 位或运行符。参与运算的如果是10进制数,则会被转换至2进...

PHP下打开phpMyAdmin出现403错误的问题解决方法

PHP下打开phpMyAdmin出现403错误的问题解决方法

安装完wamp后打开其下的phpMyAdmin也就是路径http://localhost/phpmyadmin/ 出现 看里面的代码一下明白了 解决方法直接贴图如下: 复制代码 代码...

php7安装mongoDB扩展的方法分析

本文讲述了php7安装mongoDB扩展的方法。分享给大家供大家参考,具体如下: 这里我们使用pecl命令来安装 首先来到php7的安装目录 $ /usr/local/php7/bi...