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

首頁 > 語言 > PHP > 正文

php驗證碼生成器

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

現在很多網站都有實現用戶集。然而為了防止機器人的網絡攻擊。限制登陸或者注冊是有必要的。
在注冊和登陸時強制要求輸入一個機器難以識別的字符串集是一個不錯的選擇。雖然不能解決根本問題,但至少可以增加他們的成本。

利用PHP生成驗證碼需要用到GD2庫。GD2庫引用方法網絡上有很多,不同操作系統導入方式也不同。

這段代碼運行在WINDOS服務器平臺

<?php$iC = new idCode(5,60,30);$iC->createPNG();class idCode{  private $words = array('a','b',  'c','d','e','f','g','h','i','j','k','l',  'm','n','o','p','q','r','s','t','u','v',  'w','x','y','z','A','B','C','D','E','F',  'G','H','I','J','K','L','M','N','O','P',  'Q','R','S','T','U','V','W','X','Y','Z',  '0','1','2','3','4','5','6','7','8','9');  private $fonts;  private $count;//驗證碼字符數  private $height;  private $width;  private $path = '../myfolder/fonts';  private $keys;  //構造函數  public function __construct($count,$width,$height){    $this->count = $count;    $this->getFonts();    $this->height = $height;    $this->width = $width;  }  private function getFonts(){    $dir = dir($this->path);    while(false !== ($file = $dir->read())){        if($file != '.' && $file != '..'){          $this->fonts[count($this->fonts)] = basename($file);        }    }    $dir->close();  }  private function createKeys(){    for($i = 0;$i < $this->count;$i++){      $this->keys[$i]['char'] = $this->words[rand(0,count($this->words)-1)];      //使用字體路徑標識      $this->keys[$i]['filename'] = $this->path.'//'.$this->fonts[rand(0,count($this->fonts)-1)];    }  }  public function createPNG(){    $this->createKeys();    //創建畫布以及顏色塊兒    $bg = imagecreatetruecolor($this->width + 10*2,$this->height + 3*2);//兩邊留10px空白,上下3px    $grey = imagecolorallocate($bg,155,155,155);    $blue = imagecolorallocate($bg,0x00,0x00,0xff);    //填充背景    imagefill($bg,0,0,$grey);    //添加字符    $pwidth = $this->width/$this->count;    $x;$y;    for($i = 0;$i < $this->count;$i++){      $rotation = rand(-40,40);//偏轉角度±40°      $fontsize = 33;      $width_txt;      $height_txt;      do{        $fontsize--;        $bbox = imagettfbbox($fontsize,$rotation,$this->keys[$i]['filename'],$this->keys[$i]['char']);        $width_txt = $bbox[2] - $bbox[0];//x 0 2 4 6,y1 3 5 7;左下,右下,右上,左上        $height_txt = $bbox[7] - $bbox[1];      }while($fontsize > 8 && ($height_txt > $this->height || $width_txt > $pwidth));      $fontcolor = imagecolorallocate($bg,rand(0,255),rand(0,255),rand(0,255));      $x = 8 + $pwidth*$i + $pwidth/2 - $width_txt/2;//x坐標基本位置      $y = $this->height/2 - $height_txt/2;      imagettftext($bg,$fontsize,$rotation,$x,$y,$fontcolor,$this->keys[$i]['filename'],$this->keys[$i]['char']);    }    //繪制干擾線    //根據字體酌情增加干擾線    imageline($bg,0,15,40,10,$blue);    //圖像輸出頭文件    header('Content-type:image/png');    //輸出png圖像    imagepng($bg);    //清除緩存資源    imagedestroy($bg);  }  public function checkKeys($input){    if(count($input)!=$this->count){      return 'ERROR:長度不正確.';    }else{      for($i=0;$i < $this->count;$i++){        //0 o O I l 1 校準,根據所選擇的字體確定是否需要手動校準        if($input[$i] != $this->keys[$i]['char']){          return 'SUCCESS.';        }else{          return 'ERROR:請輸入正確驗證碼.';        }      }    }  }}?>

 

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持VeVb武林網。


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

圖片精選

主站蜘蛛池模板: 柘城县| 阜南县| 镶黄旗| 乐山市| 辰溪县| 原平市| 惠州市| 金门县| 敖汉旗| 淅川县| 清丰县| 凤凰县| 紫金县| 渝北区| 白河县| 巴塘县| 米泉市| 安多县| 云梦县| 南汇区| 宝坻区| 错那县| 内丘县| 寿宁县| 筠连县| 临汾市| 高安市| 安岳县| 富宁县| 来宾市| 托克逊县| 郓城县| 濮阳市| 临江市| 顺昌县| 图片| 博罗县| 天水市| 南昌县| 蓝田县| 九龙县|