- // +---------------------------------------- ----------------------------------
- // Verification code class, objects of this class can dynamically obtain verification codes Pictures and verification code characters are saved in SESSION['code']
- // +-------------------------------- ----------------------------------------
- //Supports 4 formats of numbers, letters and Chinese characters Mix
- // +------------------------------------------------ --------------------------
- // @Author: HelloChina(sanzi0930@163.com)
- // +----- -------------------------------------------------- ------------------
- // @Date: June 7, 2012 11:03:00
- // +------------ -------------------------------------------------- ----------
- // @version 1.0
- // +-------------------------------- ------------------------------------------
- class Vcode{
- protected $ width; //Verification code width
- protected $height; //Verification code length
- protected $codeNum; //Verification code number of characters
- protected $codeType; //Verification code type
- protected $fontSize; //Character size
- protected $fontType; //Font type
- protected $codeStr; //Chinese content
- protected $strNum; //Chinese number
- protected $imageType; //Output image type
- protected $image; //Image resource
- protected $checkCode; //Verification code content
- /**
- +------------------------------------------------ --------------------------------
- * Get verification code information
- +---------- -------------------------------------------------- --------------------
- * @param integer $width Verification code width
- * @param integer $height Verification code height
- * @param integer $codeNum Verification code character Number
- * @param integer $codeType Verification code character type 1 is number 2 is letter 3 is Chinese character 4 is mixed
- * @param integer $fontSize Verification code font size
- * @param string $fontType Verification code font type
- * @param string $imageType Verification code output image type
- * @param string $codestr Chinese verification code content
- +-------------------------- -------------------------------------------------- ----
- */
- public function __construct($width=100, $height=50, $codeNum=4, $codeType=4, $fontSize=12, $fontType='heiti. ttf' ,$imageType='jpeg', $codeStr='Fuck me, it's flat. Yes, it flies well. Just think about it.'){
- $this->width = $width ;
- $this->height = $height;
- $this->codeNum = $codeNum;
- $this->codeType = $codeType;
- $this->fontSize = $fontSize;
- $this-> ;fontType = $fontType;
- $this->codeStr = $codeStr;
- $this->strNum = strlen($this->codeStr)/3-1;
- $this->imageType = $imageType;
- $this->checkCode = $this->getCheckCode();
- }
-
- //+-------------------------- -------------------------------------------------- ----
- //* Generate verification code characters
- //+---------------------------------- --------------------------------------------------
- //* @return string
- //+---------------------------------------------- ----------------------------------------
- public function __toString(){
- $string = implode( '', $this->getCheckCode());
- $_SESSION["code"]=$string; //Add to session
- $this->getImage(); //Output verification code
- return '' ;
- }
- protected function getCheckCode(){
- $string = array();
- switch($this->codeType){
- case 1:
- //numeric string
- $string = array_rand(range(0,9 ), $this->codeNum);
- break;
- case 2:
- //Large letter string
- $string = array_rand(array_flip(range('A', 'Z')), $this->codeNum );
- break;
- case 3:
- //Chinese character string
-
- for($i=0; $i<($this->codeNum); $i++){
- $start = mt_rand(0, $this ->strNum);
- $string[$i]= self::msubstr($this->codeStr,$start);
- }
- break;
- case 4:
- //Mixed strings
- for($i=0; $i<($this->codeNum); $i++){
- $rand=mt_rand(0,2);
- switch ($rand){
- case 0:
- $ascii = mt_rand(48,57);
- $string[$i] = sprintf('%c',$ascii);
- break;
Copy code
|
Verification code, PHP
This topic was moved by Xiaobei on 2015-11-18 08:23