ThinkPHP5的驗證碼操作

1、首先確認文件夾\vendor\topthink\think-captcha存在


2、顯示驗證碼的方法,我這裏是寫在:\application\admin\controller\Login.php

//顯示驗證碼
    public function show_captcha(){
        $captcha = new \think\captcha\Captcha();
        $captcha->imageW=121;
        $captcha->imageH = 32;  //圖片高
        $captcha->fontSize =14;  //字體大小
        $captcha->length   = 4;  //字符數
        $captcha->fontttf = '5.ttf';  //字體
        $captcha->expire = 30;  //有效期
        $captcha->useNoise = false;  //不添加雜點
        return $captcha->entry();
    }

3、模板文件\application\admin\view\Login\index.html中這樣引用驗證碼

<form action="/login/login_post" method="post">
<input type="text" class="input" name="captcha" placeholder="填寫右側的驗證碼" data-validate="required:請填寫右側的驗證碼"  style="width: 200px;"/>
                                <img src="/login/show_captcha" alt="" width="121" height="32" class="passcode" οnclick="this.src=this.src+'?'"/>

4、同控制器中login_post方法

//提交
    public function login_post(){
        $code=input('post.captcha');
        $captcha = new \think\captcha\Captcha();
        $result=$captcha->check($code);
        if($result===false){
            echo '驗證碼錯誤';exit;
        }
        echo '驗證碼正確,繼續';exit;
    }

大笑這樣就完成了thinkphp5中的驗證碼操作,有圖有真相



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章