laravel8中使用验证码并验证

在 Laravel 8 中使用验证码并验证的步骤如下:

1.安装验证包:

 composer require mews/captcha 

2.发布配置文件

php artisan vendor:publish --provider="Mews\Captcha\CaptchaServiceProvider"

 3.在web.php文件中添加路由

// 生成验证码图片
Route::get('captcha', 'CaptchaController@captcha')->name('captcha');

4.创建控制器中验证码方法

namespace App\Http\Controllers;
 
use Mews\Captcha\Facades\Captcha;
 
class CaptchaController extends Controller
{
    public function captcha()
    {
        return Captcha::create('default');
    }

5. 在视图中添加显示验证码的 HTML 元素和用于刷新验证码的链接:

 <form method="POST" action="/submit-form">
    @csrf
    <input type="text" name="captcha" class="form-control" placeholder="Enter the captcha">
    <img src="{{ route('captcha') }}" id="captcha-img" data-route="{{ route('captcha.refresh') }}" style="cursor: pointer;">
    <button type="submit">Submit</button>
</form>
 
<script>
    document.getElementById('captcha-img').onclick = function() {
        this.src = this.src + '?refresh=' + Math.random();
    };
</script>

6. 在表单提交的路由中添加验证码验证逻辑:

 use Illuminate\Http\Request;
use Mews\Captcha\Facades\Captcha;
 
Route::post('/submit-form', function (Request $request) {
    $request->validate([
        'captcha' => 'required|captcha'
    ]);
 
    // 验证码验证通过后的逻辑...
});

  • 4
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值