reCAPTCHA 学习

本文介绍了如何使用reCAPTCHA进行网站注册验证,包括前后端实现流程及界面定制方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

最近在帮朋友写一下小的申请页面,里头有用到验证码来防止恶意注册.
想了一下没能耐造轮子,还是拿来就用吧.找了一下,发现[url=http://recaptcha.net/]reCAPTCHA[/url] 这个非常不错,还支持声音,不过验证码看起来有点模糊.做一下笔记

先看看它的工作流程
[img]http://recaptcha.net/apidocs/captcha/recaptcha-api-diagram.gif[/img]
1. The user loads the web page with the reCAPTCHA challenge JavaScript embedded.
2. The user's browser requests a challenge from reCAPTCHA. reCAPTCHA gives the user a challenge and a token that identifies the challenge.
3. The user fills out the web page form, and submits the result to your application server, along with the challenge token.
4. reCAPTCHA checks the user's answer, and gives you back a response.
5. If true, generally you will allow the user access to some service or information. E.g. allow them to comment on a forum, register for a wiki, or get access to an email address. If false, you can allow the user to try again.


然后,你得有个帐号,然后生成一个key,跟Google的adsense类似
移步[url=https://admin.recaptcha.net/recaptcha/createsite/]这里[/url]去申请一个,然后生成你的key.

好,先在客户端加上一段js,这样就可以加载这个验证码了
这里有2种方式生成这个验证码,一个是在form中直接添加js代码

<script type="text/javascript"
src="http://api.recaptcha.net/challenge?k=<your_public_key>">
</script>

<noscript>
<iframe src="http://api.recaptcha.net/noscript?k=<your_public_key>"
height="300" width="500" frameborder="0"></iframe><br>
<textarea name="recaptcha_challenge_field" rows="3" cols="40">
</textarea>
<input type="hidden" name="recaptcha_response_field"
value="manual_challenge">
</noscript>


它将在form中生成2个input, recaptcha_challenge_field (系统生成的值) 和 recaptcha_response_field (用户输入值)
[quote]
These APIs create two form fields: recaptcha_challenge_field is a hidden field that describes the CAPTCHA which is put on the page. recaptcha_response_field is a text field where the user can enter their answer.
[/quote]

还有一种方式是用ajax的方式生成,这里有个 [url=http://recaptcha.net/fastcgi/demo/ajax]demo[/url].

在head中插入

<script type="text/javascript"
src="http://api.recaptcha.net/js/recaptcha_ajax.js"></script>

然后用类似

Recaptcha.create("6LdIEwAA......", //这个是 public_key
"recaptcha_div", // 生成位置的ID
{
theme: "red", //显示主题
callback: Recaptcha.focus_response_field //回调函数
});

来,在"recaptcha_div" 中生成的验证器.

关于界面的定制
我们可以用类似

var RecaptchaOptions = {
theme : 'white',
tabindex : 2
};

来定制界面,当然还包括语言,
需要注意的是,默认语言界面里头没有简体中文,我们可以用它的custom_translations 属性来自定义中文,我在[url=http://api.recaptcha.net/js/recaptcha.js]js代码[/url]中找到了英文的定义,然后手工翻译一下

var RecaptchaStr_en = {
visual_challenge : "Get a visual challenge",
audio_challenge : "Get an audio challenge",
refresh_btn : "Get a new challenge",
instructions_visual : "Type the two words:",
instructions_audio : "Type what you hear:",
help_btn : "Help",
play_again : "Play sound again",
cant_hear_this : "Download sound as MP3",
incorrect_try_again : "Incorrect. Try again."
};

粗糙翻译一下,欢迎拍砖:

var RecaptchaOptions = {
custom_translations : {
visual_challenge : "获取图形验证码",
audio_challenge : "获取声音验证码",
refresh_btn : "获取新的验证码",
instructions_visual : "输入2个单词:",
instructions_audio : "输入您听到的:",
help_btn : "帮助",
play_again : "重播",
cant_hear_this : "下载语音为MP3",
incorrect_try_again : "错误. 请重试."
}
};


这里还有详细的 客户端 [url=http://recaptcha.net/apidocs/captcha/client.html]API[/url],大家可以围观.

服务端部分
我写的程序是用php的,先看看php的api吧,Java的也有,但还没研究,回头再贴出来

首先你得[url=http://code.google.com/p/recaptcha/downloads/list?q=label:phplib-Latest]下载[/url]它的依赖库

然后

require_once('recaptchalib.php');
$publickey = "..."; // 申请的public key
echo recaptcha_get_html($publickey); //生成获取验证码的html代码,类似前面的用js生成



recaptcha_get_html 函数有3个参数

recaptcha_get_html($pubkey,$error ,$use_ssl)

$pubkey -- string. 必需. public key

$error -- string. 可选(默认null) 如果设置了值, reCAPTCHA 会显示这个错误的代码. 错误代码来自 ReCaptchaResponse->$error

$use_ssl -- boolean. 可选(默认false) 是否启用ssl


校验验证码

主要的方法是 recaptcha_check_answer()

require_once('recaptchalib.php');
$privatekey = "...";
$resp = recaptcha_check_answer ($privatekey,
$_SERVER["REMOTE_ADDR"],
$_POST["recaptcha_challenge_field"],
$_POST["recaptcha_response_field"]);

if (!$resp->is_valid) {
die ("The reCAPTCHA wasn't entered correctly. Go back and try it again." .
"(reCAPTCHA said: " . $resp->error . ")");
}



它有4个参数
$privkey -- string. 必需. 申请的 private key
$remoteip -- string. 必需. 用户的ip,格式:192.168.0.1
$challenge -- string. 必需recaptcha_challenge_field 字段的值,来自前台页面
$response -- string. 必需 recaptcha_response_field 字段值
Return value 返回 ReCaptchaResponse class 的实例

ReCaptchaResponse 类有2个属性
$is_valid -- boolean 校验是否有效?
$error -- string 返回的错误代码
智能网联汽车的安全员高级考试涉及多个方面的专业知识,包括但不限于自动驾驶技术原理、车辆传感器融合、网络安全防护以及法律法规等内容。以下是针对该主题的一些核心知识点解析: ### 关于智能网联车安全员高级考试的核心内容 #### 1. 自动驾驶分级标准 国际自动机工程师学会(SAE International)定义了六个级别的自动驾驶等级,从L0到L5[^1]。其中,L3及以上级别需要安全员具备更高的应急处理能力。 #### 2. 车辆感知系统的组成与功能 智能网联车通常配备多种传感器,如激光雷达、毫米波雷达、摄像头声波传感器等。这些设备协同工作以实现环境感知、障碍物检测等功能[^2]。 #### 3. 数据通信与网络安全 智能网联车依赖V2X(Vehicle-to-Everything)技术进行数据交换,在此过程中需防范潜在的网络攻击风险,例如中间人攻击或恶意软件入侵[^3]。 #### 4. 法律法规要求 不同国家地区对于无人驾驶测试及运营有着严格的规定,考生应熟悉当地交通法典中有关自动化驾驶部分的具体条款[^4]。 ```python # 示例代码:模拟简单决策逻辑 def decide_action(sensor_data): if sensor_data['obstacle'] and not sensor_data['emergency']: return 'slow_down' elif sensor_data['pedestrian_crossing']: return 'stop_and_yield' else: return 'continue_driving' example_input = {'obstacle': True, 'emergency': False, 'pedestrian_crossing': False} action = decide_action(example_input) print(f"Action to take: {action}") ``` 需要注意的是,“橙点同学”作为特定平台上的学习资源名称,并不提供官方认证的标准答案集;建议通过正规渠道获取教材并参加培训课程来准备此类资格认证考试。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值