国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 語言 > PHP > 正文

php封裝的驗證碼工具類完整實例

2024-05-04 23:51:20
字體:
來源:轉載
供稿:網友

本文實例講述了php封裝的驗證碼工具類。分享給大家供大家參考,具體如下:

<?php//驗證碼工具類class Captcha{    //屬性    private $width;    private $height;    private $fontsize;    private $pixes;    private $lines;    private $str_len;    /*     * 構造方法     * @param1 array $arr = array(),初始化屬性的關聯數組    */    public function __construct($arr = array()){      //初始化      $this->width = isset($arr['width']) ? $arr['width'] : $GLOBALS['config']['captcha']['width'];      $this->height = isset($arr['height']) ? $arr['height'] : $GLOBALS['config']['captcha']['height'];      $this->fontsize = isset($arr['fontsize']) ? $arr['fontsize'] : $GLOBALS['config']['captcha']['fontsize'];      $this->pixes = isset($arr['pixes']) ? $arr['pixes'] : $GLOBALS['config']['captcha']['pixes'];      $this->lines = isset($arr['lines']) ? $arr['lines'] : $GLOBALS['config']['captcha']['lines'];      $this->str_len = isset($arr['str_len']) ? $arr['str_len'] : $GLOBALS['config']['captcha']['str_len'];    }    /*     * 產生驗證碼圖片    */    public function generate(){      //制作畫布      $img = imagecreatetruecolor($this->width,$this->height);      //給定背景色      $bg_color = imagecolorallocate($img,mt_rand(200,255),mt_rand(200,255),mt_rand(200,255));      imagefill($img,0,0,$bg_color);      //制作干擾線      $this->getLines($img);      //增加干擾點      $this->getPixels($img);      //增加驗證碼文字      $captcha = $this->getCaptcha();      //文字顏色      $str_color = imagecolorallocate($img,mt_rand(0,100),mt_rand(0,100),mt_rand(0,100));      //寫入文字      //計算文字應該出現的起始位置      $start_x = ceil($this->width/2) - 25;      $start_y = ceil($this->height/2) - 8;      if(imagestring($img,$this->fontsize,$start_x,$start_y,$captcha,$str_color)){        //成功:輸出驗證碼        header('Content-type:image/png');        imagepng($img);      }else{        //失敗        return false;      }    }    /*     * 獲取驗證碼隨機字符串     * @return string $captcha,隨機驗證碼文字    */    private function getCaptcha(){      //獲取隨機字符串      $str = implode('',array_merge(range('a','z'),range('A','Z'),range(1,9)));      //隨機取      $captcha = '';  //保存隨機字符串      for($i = 0,$len = strlen($str);$i < $this->str_len;$i++){        //每次隨機取一個字符        $captcha .= $str[mt_rand(0,$len - 1)] . ' ';      }      //將數據保存到session      $_SESSION['captcha'] = str_replace(' ','',$captcha);      //返回值      return $captcha;    }    /*     * 增加干擾點     * @param1 resource $img    */    private function getPixels($img){      //增加干擾點      for($i = 0;$i < $this->pixes;$i++){        //分配顏色        $pixel_color = imagecolorallocate($img,mt_rand(100,150),mt_rand(100,150),mt_rand(100,150));        //畫點        imagesetpixel($img,mt_rand(0,$this->width),mt_rand(0,$this->height),$pixel_color);      }    }    /*     * 增加干擾線     * @param1 resource $img,要增加干擾線的圖片資源    */    private function getLines($img){      //增加干擾線      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);      }    }    /*     * 驗證驗證碼     * @param1 string $captcha,用戶提交的驗證碼     * @return bool,成功返回true,失敗返回false    */    public static function checkCaptcha($captcha){      //驗證碼不區分大小寫      return (strtolower($captcha) === strtolower($_SESSION['captcha']));    }}

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


注:相關教程知識閱讀請移步到PHP教程頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表

圖片精選

主站蜘蛛池模板: 大姚县| 海宁市| 上思县| 托里县| 汕尾市| 阿荣旗| 青海省| 阿克| 黄石市| 正定县| 敦化市| 乌鲁木齐市| 江源县| 康乐县| 乐业县| 永川市| 电白县| 中阳县| 东乡县| 洛浦县| 济宁市| 牙克石市| 正蓝旗| 德庆县| 都江堰市| 七台河市| 纳雍县| 吐鲁番市| 甘谷县| 揭阳市| 交城县| 华蓥市| 胶州市| 绩溪县| 行唐县| 繁峙县| 东丰县| 新泰市| 龙州县| 从江县| 芒康县|