![](http://static.blog.csdn.net/images/category_icon.jpg)
版权声明:本文为博主原创文章,未经博主允许不得转载。
在Laravel中有很多图片验证码的库可以使用,本篇介绍其中之一:gregwar/captcha,这个库比较简单,在Laravel中比较常用。下面我们就来介绍下使用细节:
首先, composer.json中如下加入配置:
<code class="json"><span class="hljs-string">"require"</span>: { ... "<span class="hljs-attribute">gregwar/captcha</span>": <span class="hljs-value"><span class="hljs-string">"1.*"</span> </span>},</code>
然后,已成习惯的命令:
composer update
接下来就可以正常使用了,根据具体的开发需求,可以有很多种方式去使用。
- 可以将验证码图片保存文件:
<code class="php"><span class="hljs-preprocessor"><?php</span>
<span class="hljs-variable">$builder</span>->save(<span class="hljs-string">'out.jpg'</span>);</code>
- 可以直接输出图片到网页:
<code class="php"><span class="hljs-preprocessor"><?php</span>
header(<span class="hljs-string">'Content-type: image/jpeg'</span>);
<span class="hljs-variable">$builder</span>->output();</code>
- 可以生成内联图片:
<code class="php"><img src=<span class="hljs-string">"<?php echo $builder->inline(); ?>"</span> /></code>
以下演示了其中一种使用方式,直接输出图片到网页。
我定义了一个Controller:
<code class="php"><span class="hljs-preprocessor"><?php</span> <span class="hljs-keyword">namespace</span> <span class="hljs-title">App</span>\<span class="hljs-title">Http</span>\<span class="hljs-title">Controllers</span>;
<span class="hljs-keyword">use</span> <span class="hljs-title">App</span>\<span class="hljs-title">Http</span>\<span class="hljs-title">Requests</span>;
<span class="hljs-keyword">use</span> <span class="hljs-title">App</span>\<span class="hljs-title">Http</span>\<span class="hljs-title">Controllers</span>\<span class="hljs-title">Controller</span>;
<span class="hljs-keyword">use</span> <span class="hljs-title">Illuminate</span>\<span class="hljs-title">Http</span>\<span class="hljs-title">Request</span>;
<span class="hljs-comment">//引用对应的命名空间</span>
<span class="hljs-keyword">use</span> <span class="hljs-title">Gregwar</span>\<span class="hljs-title">Captcha</span>\<span class="hljs-title">CaptchaBuilder</span>;
<span class="hljs-keyword">use</span> <span class="hljs-title">Session</span>;
<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">KitController</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">Controller</span> </span>{
<span class="hljs-comment">/**
* Display a listing of the resource.
*
* <span class="hljs-doctag">@return</span> Response
*/</span>
<span class="hljs-keyword">public</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">captcha</span><span class="hljs-params">(<span class="hljs-variable">$tmp</span>)</span>
</span>{
<span class="hljs-comment">//生成验证码图片的Builder对象,配置相应属性</span>
<span class="hljs-variable">$builder</span> = <span class="hljs-keyword">new</span> CaptchaBuilder;
<span class="hljs-comment">//可以设置图片宽高及字体</span>
<span class="hljs-variable">$builder</span>->build(<span class="hljs-variable">$width</span> = <span class="hljs-number">100</span>, <span class="hljs-variable">$height</span> = <span class="hljs-number">40</span>, <span class="hljs-variable">$font</span> = <span class="hljs-keyword">null</span>);
<span class="hljs-comment">//获取验证码的内容</span>
<span class="hljs-variable">$phrase</span> = <span class="hljs-variable">$builder</span>->getPhrase();
<span class="hljs-comment">//把内容存入session</span>
Session::flash(<span class="hljs-string">'milkcaptcha'</span>, <span class="hljs-variable">$phrase</span>);
<span class="hljs-comment">//生成图片</span>
header(<span class="hljs-string">"Cache-Control: no-cache, must-revalidate"</span>);
header(<span class="hljs-string">'Content-Type: image/jpeg'</span>);
<span class="hljs-variable">$builder</span>->output();
}
}</code>
下面我们可以设置相应的router访问这个验证码图片, 修改router.php:
<code class="php">Route::get(<span class="hljs-string">'kit/captcha/{tmp}'</span>, <span class="hljs-string">'KitController@captcha'</span>);</code>
现在可以通过具体的url,可以访问看到这张图片了
![](https://i-blog.csdnimg.cn/blog_migrate/44e28f0609a3727e3dced285ea169c67.webp?x-image-process=image/format,png)
验证码
表单内部写的比较简单,看看即可:
<code class="xml"><span class="hljs-tag"><<span class="hljs-title">input</span> <span class="hljs-attribute">type</span>=<span class="hljs-value">"text"</span> <span class="hljs-attribute">name</span>=<span class="hljs-value">"captcha"</span> <span class="hljs-attribute">class</span>=<span class="hljs-value">"form-control"</span> <span class="hljs-attribute">style</span>=<span class="hljs-value">"width: 300px;"</span>></span> <span class="hljs-tag"><<span class="hljs-title">a</span> <span class="hljs-attribute">onclick</span>=<span class="hljs-value">"javascript:re_captcha();"</span> ></span><span class="hljs-tag"><<span class="hljs-title">img</span> <span class="hljs-attribute">src</span>=<span class="hljs-value">"{{ URL('kit/captcha/1') }}"</span> <span class="hljs-attribute">alt</span>=<span class="hljs-value">"验证码"</span> <span class="hljs-attribute">title</span>=<span class="hljs-value">"刷新图片"</span> <span class="hljs-attribute">width</span>=<span class="hljs-value">"100"</span> <span class="hljs-attribute">height</span>=<span class="hljs-value">"40"</span> <span class="hljs-attribute">id</span>=<span class="hljs-value">"c2c98f0de5a04167a9e427d883690ff6"</span> <span class="hljs-attribute">border</span>=<span class="hljs-value">"0"</span>></span><span class="hljs-tag"></<span class="hljs-title">a</span>></span> <span class="hljs-tag"><<span class="hljs-title">script</span>></span><span class="javascript"> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">re_captcha</span>(<span class="hljs-params"></span>) </span>{ $url = <span class="hljs-string">"{{ URL('kit/captcha') }}"</span>; $url = $url + <span class="hljs-string">"/"</span> + <span class="hljs-built_in">Math</span>.random(); <span class="hljs-built_in">document</span>.getElementById(<span class="hljs-string">'c2c98f0de5a04167a9e427d883690ff6'</span>).src=$url; } </span><span class="hljs-tag"></<span class="hljs-title">script</span>></span></code>
最后就是在form提交页面验证相应验证码,库中也为我们提供了相应方法:
<code class="php"><span class="hljs-variable">$userInput</span> = <span class="hljs-variable">$request</span>->get(<span class="hljs-string">'captcha'</span>);
<span class="hljs-keyword">if</span>(<span class="hljs-variable">$builder</span>->testPhrase(<span class="hljs-variable">$userInput</span>)) {
<span class="hljs-comment">//用户输入验证码正确</span>
<span class="hljs-keyword">return</span> <span class="hljs-string">'您输入验证码正确'</span>;
} <span class="hljs-keyword">else</span> {
<span class="hljs-comment">//用户输入验证码错误</span>
<span class="hljs-keyword">return</span> <span class="hljs-string">'您输入验证码错误'</span>;
}</code>
至此,验证码就完成了。 如有疑问,欢迎回复探讨。
补充
在form表单提交验证的代码写的比较草率,给读者带来了歧义,在此有个补充(感谢 一块黄布 的回复):
$builder->testPhrase($userInput) 这里的$builder与生成验证码的$builder为同一个,如果重新new,则一直会验证失败。我们可以从源码中看到:
<code class="php"><span class="hljs-keyword">public</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">testPhrase</span><span class="hljs-params">(<span class="hljs-variable">$phrase</span>)</span>
</span>{
<span class="hljs-keyword">return</span> (<span class="hljs-variable">$this</span>->builder->niceize(<span class="hljs-variable">$phrase</span>) == <span class="hljs-variable">$this</span>->builder->niceize(<span class="hljs-variable">$this</span>->getPhrase()));
}</code>
验证码正确性判断,也可以由下列方式来验证:
<code class="php"><span class="hljs-variable">$userInput</span> = \Request::get(<span class="hljs-string">'captcha'</span>);
<span class="hljs-keyword">if</span> (Session::get(<span class="hljs-string">'milkcaptcha'</span>) == <span class="hljs-variable">$userInput</span>) {
<span class="hljs-comment">//用户输入验证码正确</span>
<span class="hljs-keyword">return</span> <span class="hljs-string">'您输入验证码正确'</span>;
} <span class="hljs-keyword">else</span> {
<span class="hljs-comment">//用户输入验证码错误</span>
<span class="hljs-keyword">return</span> <span class="hljs-string">'您输入验证码错误'</span>;
}</code>