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

2015-01-24信息快讯网

php空间不支持socket但支持curl时recaptcha的用法,需要的朋友可以参考下。

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> 
spl_autoload_register与autoload的区别详解
Window 7/XP 安装Apache 2.4与PHP 5.4 的过程详解
基于PHP字符串的比较函数strcmp()与strcasecmp()的使用详解
解析dedecms空间迁移步骤详解
解析php中mysql_connect与mysql_pconncet的区别详解
基于wordpress主题制作的具体实现步骤
Apache服务器无法使用的解决方法
setcookie中Cannot modify header information-headers already sent by错误的解决方法详解
LotusPhp笔记之:基于ObjectUtil组件的使用分析
Zend的Registry机制的使用说明
基于Zend的Captcha机制的应用
php中使用$_REQUEST需要注意的一个问题
基于curl数据采集之正则处理函数get_matches的使用
使用Apache的htaccess防止图片被盗链的解决方法
Eclipse中php插件安装及Xdebug配置的使用详解
mysqli_set_charset和SET NAMES使用抉择及优劣分析
关于mysql字符集设置了character_set_client=binary 在gbk情况下会出现表描述是乱码的情况
PhpMyAdmin出现export.php Missing parameter: what /export_type错误解决方法
openflashchart 2.0 简单案例php版
PHP句法规则详解 入门学习
PHP动态分页函数,PHP开发分页必备啦
php数组函数序列之array_search()- 按元素值返回键名
PHP隐形一句话后门,和ThinkPHP框架加密码程序(base64_decode)
php数组函数序列之each() - 获取数组当前内部指针所指向元素的键名和键值,并将指针移到下一位
php数组函数序列之prev() - 移动数组内部指针到上一个元素的位置,并返回该元素值
提示Trying to clone an uncloneable object of class Imagic的解决
PHP 小心urldecode引发的SQL注入漏洞
PHP中的strtr函数使用介绍(str_replace)
PHP 命令行工具 shell_exec, exec, passthru, system详细使用介绍
php正则表达式(regar expression)
PHP正确配置mysql(apache环境)
phpmyadmin安装时提示:Warning: require_once(./libraries/common.inc.php)错误解决办法
php数据结构 算法(PHP描述) 简单选择排序 simple selection sort
兼容firefox,chrome的网页灰度效果
PHP中用hash实现的数组
php中用foreach来操作数组的代码
php设计模式 Chain Of Responsibility (职责链模式)
php判断输入不超过mysql的varchar字段的长度范围
rephactor 优秀的PHP的重构工具
php HandlerSocket的使用
©2014-2024 dbsqp.com