PHP封裝的驗(yàn)證碼工具類定義與用法示例
本文實(shí)例講述了PHP封裝的驗(yàn)證碼工具類定義與用法。分享給大家供大家參考,具體如下:
下面分享的是我自己封裝的驗(yàn)證碼工具類,在平時的項(xiàng)目中會比較經(jīng)常用到的工具類,目前封裝的這個工具類簡易版的,如果有需要的伙伴可以拿去用,當(dāng)然我建議用之前在配置文件里增加一些選項(xiàng)信息
//驗(yàn)證碼寬度 private $width; //驗(yàn)證碼高度 private $height; //驗(yàn)證的個數(shù) private $length; //干擾點(diǎn)個數(shù) private $dots; //干擾點(diǎn)的類型 private $type; //干擾線個數(shù) private $lines; //文字 private $font;
方便在項(xiàng)目中對驗(yàn)證碼的要求進(jìn)行更改,以方便項(xiàng)目邏輯的需求,而且驗(yàn)證碼所選用的字體需要和驗(yàn)證碼工具類放在同一目錄下,否則就要在配置文件中修改字體的路徑才能實(shí)現(xiàn)驗(yàn)證碼的顯示
<?php //創(chuàng)建驗(yàn)證碼工具類 class captcha { //驗(yàn)證碼的各種參數(shù) //驗(yàn)證碼寬度 private $width; //驗(yàn)證碼高度 private $height; //驗(yàn)證的個數(shù) private $length; //干擾點(diǎn)個數(shù) private $dots; //干擾點(diǎn)的類型 private $type; //干擾線個數(shù) private $lines; //文字 private $font; //驗(yàn)證碼屬性的構(gòu)造方法 public function __construct($arr = array ()) { //將屬性賦值 $this->width = isset ($arr['width']) ? trim($arr['width']) : '270'; $this->height = isset ($arr['height']) ? trim($arr['height']) : '30'; $this->length = isset ($arr['length']) ? trim($arr['length']) : '4'; $this->dots = isset ($arr['dots']) ? trim($arr['dots']) : '81'; $this->type = isset ($arr['type']) ? trim($arr['type']) : '*'; $this->lines = isset ($arr['lines']) ? trim($arr['lines']) : '5'; $this->font = isset ($arr['font']) ? trim($arr['font']) : './cambriab.ttf'; } //創(chuàng)建驗(yàn)證碼的方法 public function captcha() { //創(chuàng)建畫布 $img = imagecreatetruecolor($this->width, $this->height); //填充顏色 //顏色資源 $color = imagecolorallocate($img, mt_rand(200, 255), mt_rand(200, 255), mt_rand(200, 255)); //填充背景 imagefill($img, 0, 0, $color); //添加干擾點(diǎn) for ($i = 0; $i < $this->dots; $i++) { //顏色資源 $dots_color = imagecolorallocate($img, mt_rand(150, 200), mt_rand(150, 200), mt_rand(150, 200)); //插入干擾點(diǎn) imagestring($img, mt_rand(1, 5), mt_rand(0, $this->width), mt_rand(0, $this->height), $this->type, $dots_color); } //添加干擾線 for ($i = 0; $i < $this->lines; $i++) { //顏色資源 $line_color = imagecolorallocate($img, mt_rand(150, 200), mt_rand(150, 200), mt_rand(150, 200)); //插入干擾線 imageline($img, mt_rand(0, $this->width), mt_rand(0, $this->height), mt_rand(0, $this->width), mt_rand(0, $this->height), $line_color); } //首先獲取驗(yàn)證碼,然后插入驗(yàn)證文字 //文字高度 $size = mt_rand(18, 20); //獲取驗(yàn)證碼 $str = $this->captchastring(); for ($i = 0; $i < strlen($str); $i++) { //顏色資源 $str_color = imagecolorallocate($img, mt_rand(50, 150), mt_rand(50, 150), mt_rand(50, 150)); //插入驗(yàn)證碼 imagettftext($img, $size, mt_rand(-45, 45), $this->width / ($this->length + 2) * ($i +1), (($this->height - $size) / 2) + $size, $str_color, $this->font, $str[$i]); } //將得到的驗(yàn)證碼存入SESSION中,便于以后的驗(yàn)證碼判斷 @ session_start(); $_SESSION['captcha'] = $str; //輸出圖片 header("content-type:image/png"); imagepng($img); //清除資源 imagedestroy($img); } //獲取隨機(jī)的驗(yàn)證內(nèi)容:A-Z,a-z,1-9 private function captchaString() { //獲取四個隨機(jī)的字符串 $str = ""; for ($i = 0; $i < $this->length; $i++) { switch (mt_rand(1, 3)) { case 1 : $str .= chr(mt_rand(49, 57)); break; case 2 : $str .= chr(mt_rand(97, 122)); break; case 3 : $str .= chr(mt_rand(65, 90)); break; } } return $str; } //判斷驗(yàn)證碼 public static function checkCaptcha($captcha) { @ session_start(); return strtoupper($captcha) === strtoupper($_SESSION['captcha']); } } //使用方法: $img = new captcha();//這里采用默認(rèn)參數(shù) $img->captcha(); ?>
運(yùn)行結(jié)果:
注:代碼中用到的字體為cambriab.ttf可完整實(shí)例代碼點(diǎn)擊此處本站下載。
更多關(guān)于PHP相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《PHP圖形與圖片操作技巧匯總》、《PHP數(shù)學(xué)運(yùn)算技巧總結(jié)》、《PHP基本語法入門教程》、《PHP運(yùn)算與運(yùn)算符用法總結(jié)》、《php面向?qū)ο蟪绦蛟O(shè)計(jì)入門教程》、《PHP數(shù)組(Array)操作技巧大全》及《php字符串(string)用法總結(jié)》
希望本文所述對大家PHP程序設(shè)計(jì)有所幫助。
- PHP基于MySQLI函數(shù)封裝的數(shù)據(jù)庫連接工具類【定義與用法】
- 常用PHP封裝分頁工具類
- php封裝的驗(yàn)證碼工具類完整實(shí)例
- php封裝的pdo數(shù)據(jù)庫操作工具類與用法示例
- PHP抓取、分析國內(nèi)視頻網(wǎng)站的視頻信息工具類
- PHP常用工具類大全附全部代碼下載
- PHP實(shí)現(xiàn)基于面向?qū)ο蟮膍ysqli擴(kuò)展庫增刪改查操作工具類
- PHP實(shí)現(xiàn)可添加水印與生成縮略圖的圖片處理工具類
- php實(shí)現(xiàn)網(wǎng)頁緩存的工具類分享
- PHP常用的類封裝小結(jié)【4個工具類】
相關(guān)文章
codeigniter實(shí)現(xiàn)get分頁的方法
這篇文章主要介紹了codeigniter實(shí)現(xiàn)get分頁的方法,涉及使用codeigniter框架查詢數(shù)據(jù)量及針對結(jié)果集進(jìn)行g(shù)et方法分頁的相關(guān)技巧,非常簡單實(shí)用,需要的朋友可以參考下2015-07-07Laravel框架中隊(duì)列和工作(Queues、Jobs)操作實(shí)例詳解
這篇文章主要介紹了Laravel框架中隊(duì)列和工作(Queues、Jobs)操作實(shí)例詳解,需要的朋友可以參考下2020-04-04PHP實(shí)現(xiàn)的鏈?zhǔn)疥?duì)列結(jié)構(gòu)示例
這篇文章主要介紹了PHP實(shí)現(xiàn)的鏈?zhǔn)疥?duì)列結(jié)構(gòu),結(jié)合具體實(shí)例形式分析了php鏈?zhǔn)疥?duì)列的定義及入隊(duì)、出隊(duì)、打印隊(duì)列等基本操作實(shí)現(xiàn)與使用方法,需要的朋友可以參考下2017-09-09php小技巧 把數(shù)組的鍵和值交換形成了新的數(shù)組,查找值取得鍵
php小技巧--把數(shù)組的鍵和值交換形成了新的數(shù)組,查找值取得鍵的實(shí)現(xiàn)方法。2011-06-06PHP異常Parse error: syntax error, unexpected T_VAR錯誤解決方法
在PHP中根本不需要使用var聲明的,但是當(dāng)一個變量作為一個類的成員變量的時候,使用var還是沒有問題的2014-05-05