>[success] # ThinkPHP 驗證碼
* 驗證碼可以防止機器人的惡意請求(惡意表單提交、破解密碼、刷票、論壇灌水......)

>[info] # 源碼下載
- Tip:公眾號回復【023】獲取源代碼

>[success] # 原生PHP 實現驗證碼
* 字體文件下載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();
```

- 技術擴展閱讀
- 第一章
- 第一節 PHP與Golang 項目案例 - 留言板
- 第二節 PHP 實現日歷功能
- 第三節 ThinkPHP 自定義分頁模板
- 第四節 WebUpload 文件上傳
- 第五節 UEditor 文本編輯器
- 第六節 ThinkPHP 驗證碼
- 第七節 百度地圖
- 第八節 PHP 接口調試工具 SocketLog
- 第九節 PHP 跟蹤調試代碼 XDebug
- 第十節 PHPExcel 表格導入和導出
- 第二章
- 十一節 實戰筆記 - Kafka 篇
- 十二節 實戰筆記 - Redis 篇
- 十三節 實戰筆記 - MySQL 篇
- 十四節 圖片轉ASCII碼圖
- 十六節 Python 視頻轉代碼視頻
- 源代碼
- 代碼2
- 十七節 GRPC PHP客戶端