thinkphp5使用官方驗證碼插件的幾個問題


1、首先使用Composer下載驗證碼插件。如果沒有composer,點擊這里下載並安裝

安裝完成后,使用以下命令修改composer配置文件,使用國內鏡像。原因你懂的。

composer config -g repo.packagist composer https://packagist.phpcomposer.com

然后打開cmd窗口,進入項目根目錄,使用以下命令下載驗證碼插件:

composer require topthink/think-captcha

下載后的路徑是: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();" />

qq%e5%9b%be%e7%89%8720161227152500

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>

完整實例源碼下載地址:http://pan.baidu.com/s/1skWnyKt


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM