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


Posted in PHP onNovember 07, 2011

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 截取字符串函数整理(支持gb2312和utf-8)
Feb 16 PHP
通过dbi使用perl连接mysql数据库的方法
Apr 16 PHP
将二维数组转为一维数组的2种方法
May 26 PHP
PHP生成短网址的3种方法代码实例
Jul 08 PHP
PHP文件锁定写入实例解析
Jul 14 PHP
ThinkPHP多语言支持与多模板支持概述
Aug 22 PHP
Drupal简体中文语言包安装教程
Sep 27 PHP
PHP中的一些常用函数收集
May 26 PHP
php实现带读写分离功能的MySQL类完整实例
Jul 28 PHP
php json中文编码为null的解决办法
Dec 14 PHP
Thinkphp5.0自动生成模块及目录的方法详解
Apr 17 PHP
tp框架(thinkPHP)实现三次登陆密码错误之后锁定账号功能示例
May 24 PHP
PHP动态分页函数,PHP开发分页必备啦
Nov 07 #PHP
php获取远程图片的两种 CURL方式和sockets方式获取远程图片
Nov 07 #PHP
php数组函数序列之array_pop() - 删除数组中的最后一个元素
Nov 07 #PHP
php数组函数序列之array_slice() - 在数组中根据条件取出一段值,并返回
Nov 07 #PHP
php数组函数序列之array_unshift() 在数组开头插入一个或多个元素
Nov 07 #PHP
php数组函数序列之array_flip() 将数组键名与值对调
Nov 07 #PHP
php数组函数序列之array_push() 数组尾部添加一个或多个元素(入栈),返回新长度。
Nov 07 #PHP
You might like
php创建session的方法实例详解
2015/01/27 PHP
PHP简单获取多个checkbox值的方法
2016/06/13 PHP
PHP后期静态绑定实例浅析
2018/12/21 PHP
PHP智能识别收货地址信息实例
2019/01/05 PHP
PHP连接SQL server数据库测试脚本运行实例
2020/08/24 PHP
javascript 用原型继承来实现对象系统
2010/03/22 Javascript
jQuery $命名冲突解决方案汇总
2014/11/13 Javascript
javascript实现playfair和hill密码算法
2014/12/07 Javascript
jQuery滚动条插件nanoscroller使用指南
2015/04/21 Javascript
JavaScript Length 属性的总结
2015/11/02 Javascript
jQuery自适应轮播图插件Swiper用法示例
2016/08/24 Javascript
jQuery插件ajaxFileUpload使用实例解析
2016/10/19 Javascript
AngularJS 限定$scope的范围实例详解
2017/06/23 Javascript
基于node.js express mvc轻量级框架实践
2017/09/14 Javascript
highCharts提示框中显示当前时间的方法
2019/01/18 Javascript
解决layui的table插件无法多层级获取json数据的问题
2019/09/19 Javascript
vue+element使用动态加载路由方式实现三级菜单页面显示的操作
2020/08/04 Javascript
简单文件操作python 修改文件指定行的方法
2013/05/15 Python
Python使用Scrapy爬取妹子图
2015/05/28 Python
python单例模式实例解析
2018/08/28 Python
Python猜数字算法题详解
2020/03/01 Python
在django中查询获取数据,get, filter,all(),values()操作
2020/08/09 Python
浅谈CSS3动画的回调处理
2016/07/21 HTML / CSS
html5自定义video标签的海报与播放按钮功能
2019/12/04 HTML / CSS
集团公司党的群众路线教育实践活动工作总结
2014/03/03 职场文书
英语故事演讲稿
2014/04/29 职场文书
甘南现象心得体会
2014/09/11 职场文书
诉讼代理人授权委托书
2014/10/11 职场文书
2015幼儿园庆元旦活动方案
2014/12/09 职场文书
优秀团支部申报材料
2014/12/26 职场文书
2015年圣诞节活动总结
2015/03/24 职场文书
2015年食堂工作总结报告
2015/04/23 职场文书
小兵张嘎观后感
2015/06/03 职场文书
幼儿园2016圣诞节活动总结
2016/03/31 职场文书
Redis如何实现分布式锁
2021/08/23 Redis
对讲机的最大通讯距离是多少
2022/02/18 无线电