驗證碼的原理和用途,想必你們都知道了吧。php
今天咱們來給tp框架裏面加入 驗證碼插件。 前端
咱們是在已經使用了composer 的基礎上增長。git
我在gitHub上找到一款:https://github.com/Gregwar/Captcha,固然你也能夠用其它的。github
首先咱們打開 composer.json shell
而後打開xshell ,來到這個目錄json
運行一下:composer install session
從新安裝一下沒有的依賴。會把captcha 下載下來。composer
+++++++++++++++++++++++++++框架
框加用的是自動載入,咱們能夠直接引入空間使用ui
use Gregwar\Captcha\CaptchaBuilder; // 這句話什麼意思 ,其實它就是一個路徑,
而後實例化
$builber = new CaptchaBuilder();
$builder->build(); //生成驗證碼圖片
$builder->save('out.jpg'); //保存驗證碼圖片
<img src="<?php echo $builder->inline(); ?>" /> //在前端輸入出圖片
$builder->getPhrase(); //獲取對應的驗證碼
$_session['phrase'] = $builder->getPhrase(); //存在session裏面
用戶的輸入和驗證碼相比較
if($builder->testPhrase($userInput)){
echo "正確";
}else{
echo "錯誤";
}
更多API,能夠參考:https://github.com/Gregwar/Captcha