>[success] ### 第四難 VerificationCode驗證碼
* 驗證碼可以防止機器人的惡意請求(惡意表單提交、破解密碼、刷票、論壇灌水......)
* 案例鏈接:https://pan.baidu.com/s/1kVcOTuv 密碼:d6j9

>[success] ### 例子2
* 字體文件下載msjh.ttf 鏈接:https://pan.baidu.com/s/1jHLpYku 密碼:el2k
~~~php
<?php
class VerificationCode
{
private $charset = "abcdefghjkmnpqrstuvwxyzABCDEFGHJKMNPQRSTUVWXYZ23456789"; //隨機因子
public $code; //驗證碼
private $codelen = 4; //驗證碼長度
private $width = 130; //寬度
private $height = 50; //高度
private $img; //圖像資源句柄
private $font; //制定字體
private $fontSize = 20; //字體大小
private $fontColor; //字體顏色
public function __construct()
{
//制定字體
$this->font = 'msjh.ttf';//注意字體路徑要寫對,否則顯示不了圖片
}
//生成驗證碼
private function createCode()
{
$len = strlen($this->charset) - 1;
for ($i = 0; $i < $this->codelen; $i++) {
$this->code .= $this->charset[mt_rand(0, $len)];
}
}
//生成背景
private function createBg()
{
$this->img = imagecreatetruecolor($this->width, $this->height);
//imagecreatetruecolor — 新建一個真彩色圖像
$color = imagecolorallocate($this->img, mt_rand(157, 255), mt_rand(157, 255), mt_rand(157, 255));
//imagecolorallocate — 為一幅圖像分配顏色
imagefilledrectangle($this->img, 0, $this->height, $this->width, 0, $color);
}
//生成文字
private function createFont()
{
//每個字符的平均寬度
$x = $this->width / $this->codelen;
for ($i = 0; $i < $this->codelen; $i++) {
//字體的顏色
$this->fontColor = imagecolorallocate($this->img, mt_rand(0, 156), mt_rand(0, 156), mt_rand(0, 156));
imagettftext($this->img, $this->fontSize, mt_rand(-10, 10), $i * $x + mt_rand(1, 3), $this->height / 1.3, $this->fontColor, $this->font, $this->code[$i]);
//imagestring($this->img,5,$i*$x+mt_rand(1,5),5,$this->code[$i],$this->fontColor);
}
}
//生成線條、雪花
private function createDisturb()
{
for ($i = 0; $i < 6; $i++) {
$color = imagecolorallocate($this->img, mt_rand(0, 156), mt_rand(0, 156), mt_rand(0, 156));
imageline($this->img, mt_rand(0, $this->width), mt_rand(0, $this->width), mt_rand(0, $this->width), mt_rand(0, $this->width), $color);
//imageline() 用 color 顏色在圖像 image 中從坐標 x1,y1 到 x2,y2(圖像左上角為 0, 0)畫一條線段。
}
for ($i = 0; $i < 100; $i++) {
$color = imagecolorallocate($this->img, mt_rand(200, 255), mt_rand(200, 255), mt_rand(200, 255));
imagestring($this->img, mt_rand(1, 5), mt_rand(0, $this->width), mt_rand(0, $this->height), '*', $color);
//imagestring — 水平地畫一行字符串
}
}
//輸出
private function outPut()
{
header("Content-Type:image/png");
imagepng($this->img);
imagedestroy($this->img);
}
//顯示驗證碼
public function showCode()
{
$this->createBg();
echo $this->code;
$this->createCode();
//$_SESSION['code'] = $this->getCode();
$this->createDisturb();
$this->createFont();
$this->outPut();
}
//獲取驗證碼
public function getCode()
{
return strtolower($this->code);
}
}
$code = new VerificationCode();
$code->showCode();
~~~

- 西天取經(九九八十一難)
- 第一難 WebUpload文件上傳
- 第二難 UEditor文本編輯器
- 第三難 ThinkPHP自定義分頁模板
- 第四難 VerificationCode驗證碼
- 第五難 遞歸 尾遞歸 迭代 回調
- 第六難 GRPC服務端和客戶端
- 第七難 PHP小筆記
- 第八難 yum安裝Apache PHP MySQL
- 第九難 高性能MySQL簡介
- 第十難 DIV+CSS簡介
- 十一難 留言板
- 十二難 正則表達式
- 十三難 PHPMailer郵件發送
- 十四難 日歷
- 十五難 百度地圖
- 十六難 加密
- 十七難 篩選url跳轉
- 十八難 圖片轉ASCII碼圖
- 十九難 環境配置
- 二十難 開發工具LiteIDE
- 二一難 開發工具2
- 二二難 準備工作
- 二三難 第一個應用程序 Hello World
- 二四難 語法結構
- 二五難 命名規則
- 二六難 變量
- 二七難 常量
- 二八難 常量計數器 iota
- 二九難 判斷語句if
- 三十難 擇語句switch
- 三一難 循環語句 for
- 三二難 遍歷 range
- 三三難 跳轉語句goto, break, continue
- 三四難 make, len, cap, new, nil
- 三五難 數組 array
- 三六難 切片 slice
- 三七難 集合 map
- 三八難 通道 channel
- 三九難 函數 function
- 四十難 結構 struct
- 四一難 方法 method
- 四二難 接口 interface
- 四三難 指針
- 四四難 異常
- 四五難 模板應用
- 四六難 Go安裝GRPC服務端和客戶端
- 四七難 安裝虛擬機軟件(VirtualBox)
- 四八難 安裝Centos7
- 四九難 --help
- 五十難 目錄文件
- 五一難 系統磁盤
- 五二難 Shell
- 五三難 Nginx安裝配置
- 五四難 PHP安裝配置
- 五五難 MySQL安裝配置
- 五六難 yum 安裝 Apache PHP MySQL
- 五七難 安裝gRPC
- 五八難 安裝memcache
- 五九難 介紹
- 六十難 DIV + CSS
- 六一難 瀏覽器兼容問題
- 六二難 書寫風格(Js中寫Html)
- 六三難 NodeJs(Express框架pug模板引擎)
- 六四難 SQL優化工具SQLAdvisor
- 六五難 JpGraph圖表
- 六六難 日期排序
- 六七難 大部分問題解決方法
- 六八難 小趣味
- 六九難 編程語言排行榜
- 七十難 PHP編輯器
- 七一難 Key
- 七二難 手動定位和自動定位當前文件位置
- 七三難 關閉代碼提示
- 七四難 配置XDebug
- 七五難 配置FTP
- 七六難 配置SSH
- 七七難 Notepad++
- 七八難 XShell 和 Xftp
- 七九難 PHPExcel導入導出
- 八十難 編程科普
- 八一難 ThinkPHP5.1新特性: 容器
- 大雷音寺(領取經書)