laravel 图片验证码

本文介绍了如何使用MewsCaptcha包在PHP项目中集成验证码功能,并详细展示了配置过程、控制器中的验证逻辑以及视图中验证码图片的显示方法。

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

命令运行:composer require mews/captcha

使用Captcha服务提供者之前还需要在config/app.php中注册服务提供者:

    

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

命令运行:php artisan vendor:publish    完成输入0

在控制器中:

use Validator;
use Gregwar\Captcha\CaptchaBuilder;
$rules = ['captcha' => 'required|captcha'];
$validator = Validator::make($request->all(), $rules);
if($validator->fails()) return view('Alert',['msg'=>'验证码错误']);

在视图中

<img src="{{url('Admin/Code?r='.time())}}"
 alt="验证码" class="pic" width="80px" height="38px" style="height:44px;cursor:pointer;"
 οnclick="this.src='{{url('Admin/Code?r=1')}}'+Math.random()"/>

所需路由

    

Route::get('Code','Admin\LoginController@getVerifyCode'); // 验证码

// 登录验证码
public function getVerifyCode(Request $request)
{
    return captcha();
}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值