色综合图-色综合图片-色综合图片二区150p-色综合图区-玖玖国产精品视频-玖玖香蕉视频

您的位置:首頁技術文章
文章詳情頁

PHP code 驗證碼生成類定義和簡單使用示例

瀏覽:80日期:2022-09-10 11:03:53

本文實例講述了PHP code 驗證碼生成類定義和簡單使用。分享給大家供大家參考,具體如下:

code.php

<?phpnamespace code;/** * Class Code */class Code{ protected $number;//驗證碼內字符個數 protected $codeType;//驗證碼樣式 protected $width;//圖像寬 protected $height;//圖像高 protected $code;//驗證碼 protected $image;//圖像資源 /** * Code constructor. * @param int $number * @param int $codeType * @param int $width * @param int $height */ public function __construct($number=5, $codeType=2, $width=100, $height=40) { $this->number = $number; $this->codeType = $codeType; $this->width = $width; $this->height = $height; $this->code = $this->createCode(); } /** * 銷毀資源 */ public function __destruct() { imagedestroy($this->image); } /** * 外部調用code時觸發 * @param $name * @return bool */ public function __get($name) { if (’code’ == $name) { return $this->$name; } else { return false; } } /** * 生成code */ protected function createCode() { switch ($this->codeType) { case 0:$code = $this->getNum();break; case 1:$code = $this->getChar();break; case 2:$code = $this->getNumChar();break; default:die(’樣式不對’); } return $code; } /** * 數字驗證碼 * @return string */ protected function getNum() { $str = join(’’, range(0,9)); return substr(str_shuffle($str), 0, $this->number); } /** * 字符驗證碼 * @return string */ protected function getChar() { $str = join(’’, range(’a’, ’z’)); $str = $str . strtoupper($str); return substr(str_shuffle($str), 0, $this->number); } /** * 字符和數字混合驗證碼 * @return string */ protected function getNumChar() { $num = join(’’, range(0, 9)); $str = join(’’, range(’a’, ’z’)); $str_big = strtoupper($str); $numChar = $num . $str . $str_big; return substr(str_shuffle($numChar), 0, $this->number); } /** * 生成圖像 */ protected function createImage() { $this->image = imagecreatetruecolor($this->width, $this->height); } /** * 填充背景色 */ protected function fillColor() { imagefill($this->image, 0, 0, $this->lightColor()); } /** * 淺顏色 * @return int */ protected function lightColor() { return imagecolorallocate($this->image, mt_rand(170, 255), mt_rand(170, 255), mt_rand(170, 255)); } /** * 深顏色 * @return int */ protected function darkColor() { return imagecolorallocate($this->image, mt_rand(0, 120), mt_rand(0, 120), mt_rand(0, 120)); } /** * 添加驗證碼字符 */ protected function drawChar() { $width = ceil($this->width/$this->number); for ($i = 0; $i < $this->number; $i++) { $x = mt_rand($i * ($width - 5), ($i + 1) * ($width - 5)); $y = mt_rand(0, $this->height - 15); imagechar($this->image, 5, $x, $y, $this->code[$i], $this->darkColor()); } } /** * 添加干擾點 */ protected function drawDisturb() { for ($i= 0; $i < 100; $i++) { imagesetpixel($this->image, mt_rand(0, $this->width), mt_rand(0, $this->height), $this->darkColor()); } } /** * 添加干擾線 */ protected function drawArc() { for ($i = 0; $i < $this->number - 3; $i++) { imagearc($this->image, mt_rand(5, $this->width), mt_rand(5, $this->height), mt_rand(5, $this->width), mt_rand(5, $this->height),mt_rand(0, 70), mt_rand(300, 360), $this->darkColor()); } } /** * 輸出顯示 */ protected function show() { header(’Content-Type:image/png’); imagepng($this->image); } /** * 外部image */ public function outImage() { $this->createImage();//創建畫布 $this->fillColor();//填充背景色 $this->drawChar();//添加驗證字符 $this->drawDisturb();//添加干擾點 $this->drawArc();//添加干擾線 $this->show();//輸出 }}

展示驗證碼。。保存驗證碼和過期時間

<?phpinclude ’./code/Code.php’; $code = new codeCode();$code->outImage();session_start();$_SESSION[’code’] = [ ’code’ => $code->code, ’exp_time’ => time() + (60 * 60 * 10),];

更多關于PHP相關內容感興趣的讀者可查看本站專題:《PHP圖形與圖片操作技巧匯總》、《PHP數組(Array)操作技巧大全》、《PHP數據結構與算法教程》、《php程序設計算法總結》、《PHP數學運算技巧總結》、《php字符串(string)用法總結》及《php常見數據庫操作技巧匯總》

希望本文所述對大家PHP程序設計有所幫助。

標簽: PHP
相關文章:
主站蜘蛛池模板: 国产亚洲视频在线播放大全 | 国产精品无打码在线播放9久 | 国产99久久精品 | 欧美色视频日本片高清在线观看 | 亚洲 欧美 日韩 在线 中文字幕 | 久久久久久一级毛片免费无遮挡 | 亚洲性免费 | 美女又黄又免费视频 | 亚洲国产高清在线 | 国产成人精品男人免费 | 国产99视频精品一区 | 91精品自在拍精选久久 | 99爱免费观看视频在线 | 亚洲欧洲日产v特级毛片 | 欧美日韩亚洲国内综合网俺 | 国内精品小视频 | 久久久久国产精品美女毛片 | 天堂8在线天堂资源bt | 91国在线啪精品一区 | 久久国产精品国产精品 | 日本美女高清在线观看免费 | 午夜两性视频 | 中文字幕一区二区三区视频在线 | 亚洲高清成人欧美动作片 | 男人操女人逼逼视频 | 国产在线播放一区 | 成人性毛片 | 亚洲综合资源 | 欧美自拍视频在线 | 国产国模福利视频 | 国产2021中文天码字幕 | 精品视频免费在线 | 久久精品女人毛片国产 | 亚洲波多野结衣日韩在线 | 99re5久久在热线播放 | 久久精品最新免费国产成人 | 日韩成人精品日本亚洲 | 久久99爰这里有精品国产 | 国产一区在线看 | 免费一级a毛片在线播放 | 亚洲欧美日韩中文字幕在线一 |