我們專注于高端品牌網(wǎng)站創(chuàng)意設計與開發(fā)
1、首先使用Composer下載驗證碼插件。如果沒有composer,點擊這里下載并安裝。
安裝完成后,使用以下命令修改composer配置文件,使用國內(nèi)鏡像。原因你懂的。
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,
// 驗證碼位數(shù)
'length' => 4,
// 驗證成功后是否重置
'reset' => true
],
更詳細的參數(shù)配置,參考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參數(shù)設置為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://www.lt-ad.com/new/188.html
本站文章為深圳網(wǎng)站建設·源美網(wǎng)絡原創(chuàng)策劃,如有版權(quán)糾紛或者違規(guī)問題,請聯(lián)系我們刪除,謝謝!
上一篇: 什么是微信小程序呢?微信小程序的趨勢
下一篇: 嚴格界定與靈活的分類法?
售后保障
承諾任何問題1小時內(nèi)解決數(shù)據(jù)備份
更安全、更高效、更穩(wěn)定價格公道精準
項目經(jīng)理精準報價不弄虛作假合作無風險
重合同講信譽,無效全額退款