Laravel中使用验证码

预览

Preview

安装

composer.json中添加验证码的引用

{

    "require": {

        "laravel/framework": "5.0.*",

        "mews/captcha": "~2.0"

    },

    "minimum-stability": "dev"

}

或者是

composer require mews/captcha

接着就是运行下面的命令来更新库的依赖

composer update

或者

 composer install

在windows系统中,必须在php.ini开启GD2 DLL拓展 php_gd2.dll,同时还必须开启php_fileinfo.dllphp_mbstring.dll

使用

config/app.php 中注入验证码服务提供者。

'providers' => [
    // ...
    'Mews\Captcha\CaptchaServiceProvider',
]

for Laravel 5.1+

'providers' => [
    // ...
    Mews\Captcha\CaptchaServiceProvider::class,
]

找到aliases keyconfig/app.php

'aliases' => [
     // ...
    'Captcha' => 'Mews\Captcha\Facades\Captcha',
]

for Laravel 5.1+

 'aliases' => [
        // ...
        'Captcha' => Mews\Captcha\Facades\Captcha::class,
    ]

配置

可以自定义验证码的样式以及输入字符的数量

将配置文件拷贝到config目录下
$ php artisan vendor:publish

配置文件路径
config/captcha.php

return [
    'default'   => [
        'length'    => 5,
        'width'     => 120,
        'height'    => 36,
        'quality'   => 90,
    ],
    // ...
];

具体的使用例子

 <div class="form-group {{ $errors->has('captcha') ? ' has-error' : '' }}">
    <label for="captcha" class="col-md-4 control-label">验证码</label>

    <div class="col-md-6">
        <input id="captcha" class="form-control" name="captcha" >

        <img class="thumbnail captcha" src="{{ captcha_src('flat') }}" onclick="this.src='/captcha/flat?'+Math.random()" title="点击图片重新获取验证码">

        @if ($errors->has('captcha'))
            <span class="help-block">
            <strong>{{ $errors->first('captcha') }}</strong>
        </span>
        @endif
    </div>
</div>
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值