温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

thinkphp5如何用composer下载验证码类提示错误

发布时间:2021-09-07 10:03:13 来源:亿速云 阅读:146 作者:小新 栏目:web开发

这篇文章给大家分享的是有关thinkphp5如何用composer下载验证码类提示错误的内容。小编觉得挺实用的,因此分享给大家做个参考,一起跟随小编过来看看吧。

1、首先使用Composer下载验证码插件。如果没有composer,点击这里下载并安装

安装完成后,使用以下命令修改composer配置文件,使用国内镜像。原因你懂的。
composer config -g repo.packagist composer https://packagist.phpcomposer.com

然后打开cmd窗口,进入项目根目录,比如在(D:/www/blog,先cd到这个目录)使用以下命令下载验证码插件:
composer require topthink/think-captcha 1 (注意:thinkphp5.0版本的验证码要用/think-captcha 1.0的版本,不然会安装失败)安装失败提示如下图:thinkphp5如何用composer下载验证码类提示错误

下载后的路径是:vendor\topthink\think-captcha\src


2、确保项目配置文件application/config.php中,以下两项配置为true:

 'auto_start'   => true,
 'url_route_on' => true,

然后在配置文件中添加:

'captcha'  => [
    // 验证码字符集合
    'codeSet'  => '2345678abcdefhijkmnpqrstuvwxyzABCDEFGHJKLMNPQRTUVWXY', 
    // 验证码字体大小(px)
    'fontSize' => 20, 
    // 是否画混淆曲线
    'useCurve' => true, 
     // 验证码图片高度
    'imageH'   => 30,
    // 验证码图片宽度
    'imageW'   => 100, 
    // 验证码位数
    'length'   => 4, 
    // 验证成功后是否重置        
    'reset'    => true
],

更详细的参数配置,参考vendor\topthink\think-captcha\src\Captcha.php类文件中的说明。

3、显示验证码:

<img src="{:captcha_src()}" onclick="this.src='{:captcha_src()}?x='+Math.random();" />

4、几个问题:

访问http://192.168.0.102/3/public/,验证码无法显示。

但是访问http://192.168.0.102/3/public/index.php,验证码正常显示。

解决方法:

打开vendor\topthink\think-captcha\src\helper.php文件,查找captcha_src方法,设置一个固定路径,比如我是放在web目录下的3文件夹里。

function captcha_src($id = "")
{
    $root= \think\Url::root('/3/public/index.php'); 
    return \think\Url::build('/captcha' . ($id ? "/{$id}" : ''));
}

另外,useImgBg参数设置为true后,会找不到图片背景。修改vendor\topthink\think-captcha\src\Captcha.php的_background方法,把$path变量修改为$path = dirname(FILE) . ‘/verify/bgs/’;

———————————————————————————-

完整例子源码:

控制器

<?php
namespace app\index\controller;
use think\Controller;
class Index extends Controller
{
    public function index()
    {
        return $this->fetch();
    }
    public function checkcode() 
    {
        $code=input('yanzhengma');
        if(!captcha_check($code))
        {
         echo "验证码错误!";
        } else {
         echo "验证通过!";
        }
    }
}
?>

视图index.html:

<form method="post" action="index.php/index/index/checkcode">
<input name="yanzhengma" type="text" />
<img src="{:captcha_src()}" onclick="this.src='{:captcha_src()}?x='+Math.random();" />
<input name="tijiao" type="submit" />
</form>

感谢各位的阅读!关于“thinkphp5如何用composer下载验证码类提示错误”这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,让大家可以学到更多知识,如果觉得文章不错,可以把它分享出去让更多的人看到吧!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI