在网站开发中使用频率最高的工具之一便是验证码
,网络上各种验证码鱼龙混杂,本文精选三种优秀的验证码服务,希望能给读者以帮助。
1. #### Google recaptcha - 点击图片进行验证(免费)
2. #### 极验(本文在Laravel中通过极验验证码包进行安装使用)- 点击图片验证、滑动验证
3. #### Laravel的验证码库gregwar/captcha - 输入图片验证码进行验证
Google recaptcha
reCaptcha是Google开发的验证码工具,使用十分简单 https://developers.google.com/recaptcha/。
1)登陆你的Google账户,没有的话是用不了的。
2)在这里来申请一对key https://www.google.com/recaptcha/admin ,如下图
一个Google账户可以申请很多key,第一个label随便填,第二个是你的域名。我这里本地测试,直接输入localhost即可。
3)申请成功后可以看到两个key,一个是Site key,可以公开,一个是你自己的私key
4)Client端设置:写一个HTML网页,需要带一个form,用来提交表单。
<html>
<head>
<title>reCAPTCHA demo: Simple page</title>
<script src="https://www.google.com/recaptcha/api.js" async defer></script>
</head>
<body>
<form action="./test.php" method="GET">
<input type="email" name="email">
<input type="password" name="password">
<div class="g-recaptcha" data-sitekey="yoursitekey"></div>
<br/>
<input type="submit" value="Submit">
</form>
</body>
</html>
这样的的主页将产生:
点击蓝色的方框即可开始验证:
5)验证完毕并提交表单后,将会返回一个g-recaptcha-response 值。此时查看网页代码会发现class=”g-recaptcha”的div里会多了一个name为g-recaptcha-response的input,它会和你的表单输入一起提交到服务端,它用来和Google服务器通信验证验证码是否正确。
6)服务端用你的秘钥验证g-recaptcha的值
$post_data = array(
'secret' => 'yoursecretkey',
'response' => $_POST["g-recaptcha-response"]
);
// 这里的send_post()函数使用curl来实现post
$recaptcha_json_result = send_post('https://www.google.com/recaptcha/api/siteverify', $post_data);
$recaptcha_result = json_decode($recaptcha_json_result);
将这个g-recaptcha值通过post方式发送到 https://www.google.com/recaptcha/api/siteverify 将获得一个json格式结果:
极验
极验验证码提供了一个安全可靠的滑动验证码体系,让网站开发更加安全。
接入极验验证码的过程并没有想象中的那么简单,如果想在Laravel 5中使用,可以使用Laravel 5的极验验证码包LaravelGeetest,支持 Laravel 5.0 及以上版本。
详情见 极验(Geetest) Laravel 5 集成开发包,让验证更安全
效果:
Laravel的验证码库gregwar/captcha
在Laravel中有很多图片验证码的库可以使用,我们使用其中之一:gregwar/captcha,这个库比较简单,在Laravel中比较常用
详情见Laravel的验证码库gregwar/captcha
效果: