TP5 使用驗證碼功能

來源:互聯網
上載者:User

標籤:style   增加   圖片   需要   horizon   http   tp5   說明   func   

  工作中後台開發使用的是 TP5,但是對文法不是很熟悉,總是看著手冊寫代碼。當時做 Java 的時候也是這樣,很多文法需要靠百度。不是不能寫代碼,但是這樣的效率感覺不高,沒有行雲流水的感覺,要是能有聊天時打字的感覺就好多了。不熟悉就得多學習,但是學完經常就忘記了,所以部落格就當是記錄了。

 

  TP5的驗證碼在 /vendor/topthink/think-captcha 目錄下,檔案分別是 Captcha.php 、CaptchaController.php 和 helper.php 三個檔案。可以直接通過 http://localhost/項目名稱/public/index.php/captcha 來進行訪問。

 

1、在頁面上顯示驗證碼

  插入一個 img 的 html 標籤:

1 <img src="/public/index.php/captcha" alt="點擊更新驗證碼" onclick="reloadcode(this);"/>

 

 2、點擊驗證碼圖片時更新驗證碼

  插入一段 js 代碼:

1 // 重新整理驗證碼2 function reloadcode(obj) {3     obj.src = ‘/public/index.php/captcha?id=‘ + Math.random();4 }

 

3、驗證碼顯示大小的設定

  在 /application/config.php 檔案的末尾增加配置如下:

1 ‘captcha‘   => [2     ‘imageH‘    => 50,3     ‘imageW‘    => 200,4 ],

 

4、判斷驗證碼

  登入時將資料提交的地址,代碼如下:

1 <form class="form form-horizontal" action="{:url(‘login/check‘)}" method="post">

 

5、判斷驗證碼

  判斷驗證碼是否正確,在提交的 Controller 中增加對應的判斷代碼:

1 public function check()2 {3     $data = input(‘post.‘);4     if (!captcha_check($data[‘code‘])) {5         $this->error(‘驗證碼不正確‘);6     } else {7         $this->success(‘OK‘);8     }9 }

 

TP5 驗證碼的詳細說明地址:https://www.kancloud.cn/manual/thinkphp5/154295

 

TP5 使用驗證碼功能

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.