验证码主要是为了防止外部用户恶意自动填写内容,但是也增加了录入的步骤。特别是管理员录入文章也需要填写验证码,就特别的麻烦。
期望管理员录入时,能免填验证码。如何做到呢?
1、在config里维护免填验证码的账号
2、页面模板里检测到当前用户在免填验证码清单里,就不显示验证码输入框
3、controll提交数据的方法里,检测到当前用户在免填验证码清单里,就不进行验证码的校验。
代码如下:
[
'name' => 'nocaptchusers',
'title' => '免验证码用户',
'type' => 'string',
'content' => [],
'value' => 'feige,352258802',
'rule' => '',
'msg' => '',
'tip' => '这些用户发表内容不需要验证码',
'ok' => '',
'extend' => '',
],
关键代码如下:
<!-- edit tonny 20241101:如果是免验证用户,不需要验证码 -->
<?php
$nocaptchuserList = explode(',', $askConfig['nocaptchusers']??'');
$currentUserName = $askConfig['user']["username"];
if(!in_array($currentUserName, $nocaptchuserList)):
?>
<div class="form-group">
<label class="control-label">验证码</label>
....
</div>
<?php endif;?>
关键代码如下:
//edit tonny 20241101:如果免验证码用户,则不验证验证码
//$this->captcha('postarticle');
$nocaptchuserList = explode(',', $config['nocaptchusers']??'');
$currentUserName = $this->auth->username;
if(!in_array($currentUserName, $nocaptchuserList)){
$this->captcha('postarticle');
}
以上是文章模块的调整,问题和回答的也按此方法实现即可。