TP6实现前后端分离的图片验证码,验证码以接口形式返回

1. 验证码接口

$captcha = captcha();
$data['data'] = [
    'img' => "data:image/png;base64," . base64_encode($captcha->getData()),
    'key' => uniqid()
];
$data['status'] = 200;
return json($data);

2.验证

$code = '用户输入的验证码';
$key = session('captcha.key');
if(!password_verify(mb_strtolower($code, 'UTF-8'), $key)){
    //验证码错误
}
posted @ 2024-05-21 13:30  _迷途  阅读(11)  评论(0编辑  收藏  举报