php封裝的驗證碼工具類完整執行個體_php技巧

來源:互聯網
上載者:User

本文執行個體講述了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圖形與圖片操作技巧匯總》、《PHP基本文法入門教程》、《PHP運算與運算子用法總結》、《php物件導向程式設計入門教程》、《PHP網路編程技巧總結》、《PHP數組(Array)操作技巧大全》、《php字串(string)用法總結》、《php+mysql資料庫操作入門教程》及《php常見資料庫操作技巧匯總》

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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.