這次帶給大家php生成複雜驗證碼有哪些方法,php產生複雜驗證碼的注意事項有哪些,以下就是實戰案例,一起來看一下。
常見的軟體不容易自動辨識的驗證碼做法有以下3種:
1、字型變型(一般透過演算法,進行扭曲)
#2、字型黏貼(這裡面以qq驗證碼代表了,目前網路上還是很難找到,破解qq驗證碼的)
3 、幹擾線,雜訊(這種辨識起來相當容易,很容易被程式自動化辨識)
對於上面提到,第1,2 二種方法,在辨識時候,是比較困難的。
具體實作程式碼:
<?php /** *带文字旋转,倾斜,黏贴,加正弦干扰线验证码* */ class Utils_Caption { var $Width = 60; //图片宽 var $Height = 30; //图片高 var $Length = 4; //验证码位数 var $BgColor = "#FFFFFF"; //背景色 var $TFonts = array("font.ttf"); var $TFontSize=array(17,20); //字体大小范围 var $TFontAngle=array(-20,20); //旋转角度 var $Chars = "0123456789"; //验证码范围(字母数字) var $Code = array(); //验证码 var $Image = ""; //图形对象 var $FontColors=array('#f36161','#6bc146','#5368bd'); //字体颜色,红绿蓝 var $TPadden = 0.75;///字符间距,多少个字符 var $Txbase = 5;///x轴两边距离 var $Tybase =5 ;///y轴两边距离 var $TLine =true; ///画干扰线 public function RandRSI() ///生成验证码 { $this->TFontAngle=range($this->TFontAngle[0],$this->TFontAngle[1]); $this->TFontSize=range($this->TFontSize[0],$this->TFontSize[1]); $arr=array(); $Chars=$this->Chars; $TFontAngle=$this->TFontAngle; $TFontSize=$this->TFontSize; $FontColors=$this->FontColors; $code=""; $font=dirname(FILE)."/font/".$this->TFonts[0]; $charlen=strlen($Chars)-1; $anglelen=count($TFontAngle)-1; // 角度范围 $fontsizelen=count($TFontSize)-1; // 角度范围 $fontcolorlen=count($FontColors)-1; // 角度范围 for($i=0;$iLength;$i++) ///得到字符与颜色 { $char=$Chars[rand(0,$charlen)]; ///得到字符 $angle=$TFontAngle[rand(0,$anglelen)]; ///旋转角度 $fontsize=$TFontSize[rand(0,$fontsizelen)]; ///字体大小 $fontcolor=$FontColors[rand(0,$fontcolorlen)]; ///字体大小 $bound=$this->_calculateTextBox($fontsize,$angle,$font,$char); ///得到范围 $arr[]=array($fontsize,$angle,$fontcolor,$char,$font,$bound); ///得到矩形框 $code.=$char; } $this->Code=$arr; //验证码 return $code; } public function Draw() ///画图 { if(empty($this->Code)) $this->RandRSI(); $codes=$this->Code; ///用户验证码 $wh=$this->_getImageWH($codes); $width=$wh[0]; $height=$wh[1]; ///高度 $this->Width=$width; $this->Height=$height; $this->Image = imageCreate( $width, $height ); $image=$this->Image; $back = $this->_getColor2($this->_getColor( $this->BgColor)); ///背景颜色 imageFilledRectangle($image, 0, 0, $width, $height, $back); ///填充背景 $TPadden=$this->TPadden; $basex=$this->Txbase; $color=null; foreach ($codes as $v) ///逐个画字符 { $bound=$v[5]; $color=$this->_getColor2($this->_getColor($v[2])); imagettftext($image, $v[0], $v[1], $basex, $bound['height'],$color , $v[4], $v[3]); $basex=$basex+$bound['width']*$TPadden-$bound['left'];///计算下一个左边距 } $this->TLine?$this->_wirteSinLine($color,$basex):null; ///画干扰线 header("Content-type: image/png"); imagepng( $image); imagedestroy($image); } /** *通过字体角度得到字体矩形宽度* * * @param int $font_size 字体尺寸 * @param float $font_angle 旋转角度 * @param string $font_file 字体文件路径 * @param string $text 写入字符 * @return array 返回长宽高 */ private function _calculateTextBox($font_size, $font_angle, $font_file, $text) { $box = imagettfbbox($font_size, $font_angle, $font_file, $text); $min_x = min(array($box[0], $box[2], $box[4], $box[6])); $max_x = max(array($box[0], $box[2], $box[4], $box[6])); $min_y = min(array($box[1], $box[3], $box[5], $box[7])); $max_y = max(array($box[1], $box[3], $box[5], $box[7])); return array( 'left' => ($min_x >= -1) ? -abs($min_x + 1) : abs($min_x + 2), 'top' => abs($min_y), 'width' => $max_x - $min_x, 'height' => $max_y - $min_y, 'box' => $box ); } private function _getColor( $color ) //#ffffff { return array(hexdec($color[1].$color[2]),hexdec($color[3].$color[4]),hexdec($color[5].$color[6])); } private function _getColor2( $color ) //#ffffff { return imagecolorallocate ($this->Image, $color[0], $color[1], $color[2]); } private function _getImageWH($data) { $TPadden=$this->TPadden; $w=$this->Txbase; $h=0; foreach ($data as $v) { $w=$w+$v[5]['width']*$TPadden-$v[5]['left']; $h=$h>$v[5]['height']?$h:$v[5]['height']; } return array(max($w,$this->Width),max($h,$this->Height)); } //画正弦干扰线 private function _wirteSinLine($color,$w) { $img=$this->Image; $h=$this->Height; $h1=rand(-5,5); $h2=rand(-1,1); $w2=rand(10,15); $h3=rand(4,6); for($i=-$w/2;$i<p style="text-align: left;">DEMO程式碼:</p><pre class="brush:php;toolbar:false">$rsi = new Utils_Caption(); $rsi->TFontSize=array(15,17); $rsi->Width=50; $rsi->Height=25; $code = $rsi->RandRSI(); session_start(); $_SESSION["CHECKCODE"] = $code; $rsi->Draw();
運行效果:
以上是php產生複雜驗證碼有哪些方法的詳細內容。更多資訊請關注PHP中文網其他相關文章!

php把负数转为正整数的方法:1、使用abs()函数将负数转为正数,使用intval()函数对正数取整,转为正整数,语法“intval(abs($number))”;2、利用“~”位运算符将负数取反加一,语法“~$number + 1”。

实现方法:1、使用“sleep(延迟秒数)”语句,可延迟执行函数若干秒;2、使用“time_nanosleep(延迟秒数,延迟纳秒数)”语句,可延迟执行函数若干秒和纳秒;3、使用“time_sleep_until(time()+7)”语句。

php字符串有下标。在PHP中,下标不仅可以应用于数组和对象,还可应用于字符串,利用字符串的下标和中括号“[]”可以访问指定索引位置的字符,并对该字符进行读写,语法“字符串名[下标值]”;字符串的下标值(索引值)只能是整数类型,起始值为0。

php除以100保留两位小数的方法:1、利用“/”运算符进行除法运算,语法“数值 / 100”;2、使用“number_format(除法结果, 2)”或“sprintf("%.2f",除法结果)”语句进行四舍五入的处理值,并保留两位小数。

在php中,可以使用substr()函数来读取字符串后几个字符,只需要将该函数的第二个参数设置为负值,第三个参数省略即可;语法为“substr(字符串,-n)”,表示读取从字符串结尾处向前数第n个字符开始,直到字符串结尾的全部字符。

判断方法:1、使用“strtotime("年-月-日")”语句将给定的年月日转换为时间戳格式;2、用“date("z",时间戳)+1”语句计算指定时间戳是一年的第几天。date()返回的天数是从0开始计算的,因此真实天数需要在此基础上加1。

方法:1、用“str_replace(" ","其他字符",$str)”语句,可将nbsp符替换为其他字符;2、用“preg_replace("/(\s|\ \;||\xc2\xa0)/","其他字符",$str)”语句。

查找方法:1、用strpos(),语法“strpos("字符串值","查找子串")+1”;2、用stripos(),语法“strpos("字符串值","查找子串")+1”。因为字符串是从0开始计数的,因此两个函数获取的位置需要进行加1处理。


熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

AI Hentai Generator
免費產生 AI 無盡。

熱門文章

熱工具

SublimeText3 Mac版
神級程式碼編輯軟體(SublimeText3)

MinGW - Minimalist GNU for Windows
這個專案正在遷移到osdn.net/projects/mingw的過程中,你可以繼續在那裡關注我們。 MinGW:GNU編譯器集合(GCC)的本機Windows移植版本,可自由分發的導入函式庫和用於建置本機Windows應用程式的頭檔;包括對MSVC執行時間的擴展,以支援C99功能。 MinGW的所有軟體都可以在64位元Windows平台上運作。

Atom編輯器mac版下載
最受歡迎的的開源編輯器

Dreamweaver CS6
視覺化網頁開發工具

VSCode Windows 64位元 下載
微軟推出的免費、功能強大的一款IDE編輯器