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

首頁 > 編程 > PHP > 正文

php數字及數字運算驗證碼

2020-03-22 18:57:54
字體:
來源:轉載
供稿:網友
本篇文章主要介紹php數字及數字運算驗證碼,感興趣的朋友參考下,希望對大家有所幫助。

具體如下:

1. 數字驗證碼:

<?php//第一個實例是數字驗證碼,最常見的驗證碼。多少個數字可以由自己決定。//$num是生成的驗證碼包含幾個數字getValidate(4,60,20);function getValidate($num,$w,$h){  $code = "";  for($i=0;$i<$num;$i++){    $code .= rand(0,9);  }  //code變量最后是$num個數字,并且是字符串。因襲如果是生成四位的數字驗證碼,可以用rand(1000,9999)生成  Header("Content-type:image/PNG");  $img = imagecreate($w,$h);//創建圖片,長寬參數是一開始定義好的。  $black = imagecolorallocate($img,0,0,0);//定義黑色  $gray = imagecolorallocate($img,200,200,200);//定義灰色  $bgcolor = imagecolorallocate($img,255,255,255);//背景色白色  imagefill($img,0,0,$gray);  //imagefill($image,ing $x,int $y,int $color),在image圖像的坐標x,y處用color顏色執行區域填充  //給驗證碼畫上邊框,感覺驗證碼美美的,其實呢畫邊框就是給他畫上一個矩形  imagerectangle($img,0,0,$w-1,$h-1,$black);//用黑色的框框  /*imagestring($image,$font,$x,$y,$s,$col)  用col顏色將字符串s畫到image所代表的圖像的x,y處(xy是字符串的左上角坐標),  整幅圖像的左上角為(0,0)如果font是1,2,3,4,5則使用內置字體*/  //一下是畫四個數字的方法,讓四個字符位置都隨機等  $strx = rand(5,10);  for($i = 0;$i < $num;$i++){    $strops = rand(1,6);    imagestring($img,5,$strx,$strops,substr($code,$i,1),$black);    $strx += rand(8,12);    //strx是橫坐標,需要每一次加上之前的坐標,保證不會重疊。縱坐標不管  }  //substr() 函數返回字符串的一部分,substr(string,start,length),要截取的字符串,start開始位置,length截取的長度  //生成好了數字,下面要給驗證碼區域一些干擾,防止一些工具可以自動識別  //1.方法一:給背景加上很多噪點  //imagesetpixel($image,$x,$y,$color),在image圖像中用color顏色在x,y坐標(圖像左上角為0,0)上畫一個點  for($i = 0;$i<80;$i++){    imagesetpixel($img, rand(0,$w), rand(0,$h), $black);//這些點分布在這個背景里面  }  //2.方法二:繪制幾條虛線  $style = array($black,$gray);  imagesetstyle($img, $style);  //imagesetstyle($image,$style),設定畫線的風格,像素組成的數組  $y1 = rand(0,$h);  $y2 = rand(0,$h);  $y3 = rand(0,$h);  $y4 = rand(0,$h);  imageline($img,0,$y1,$w,$y2,IMG_COLOR_STYLED);  imageline($img,0,$y3,$w,$y4,IMG_COLOR_STYLED);  imagepng($img);  imagedestroy($img);}?>

2. 數字運算驗證碼:

<?phpgetValidate(100,30);function getValidate($w,$h){  $img = imagecreate($w,$h);  $gray = imagecolorallocate($img,255,255,255);  $black = imagecolorallocate($img,rand(0,200),rand(0,200),rand(0,200));  $red = imagecolorallocate($img, 255, 0, 0);  $white = imagecolorallocate($img, 255, 255, 255);  $green = imagecolorallocate($img, 0, 255, 0);  $blue = imagecolorallocate($img, 0, 0, 255);  imagefilledrectangle($img, 0, 0, 100, 30, $black);  for($i = 0;$i < 80;$i++){    imagesetpixel($img, rand(0,$w), rand(0,$h), $gray);  }  $num1 = rand(1,99);  $num2 = rand(1,99);  imagestring($img, 5, 5, rand(1,10), $num1, $red);  imagestring($img,5,30,rand(1,10),getRand(), $white);  imagestring($img,5,45,rand(1,10),$num2, $green);  imagestring($img,5,65,rand(1,10),"=", $blue);  imagestring($img,5,80,rand(1,10),"?", $red);  header("content-type:image/png");  imagepng($img);  imagedestroy($img);}function getRand(){  $code = rand(0,1);  switch ($code) {    case 0:      return "+";      break;    case 1:      return "-";      break;    default:      # code...      break;  }}?>

總結:以上就是本篇文的全部內容,希望能對大家的學習有所幫助。

相關推薦:

php采用鏈式調用的方式連續調用函數的使用方法

php使用define定義常量的使用方法

php實現json編碼轉換的方法

以上就是php數字及數字運算驗證碼的詳細內容,更多請關注 其它相關文章!

鄭重聲明:本文版權歸原作者所有,轉載文章僅為傳播更多信息之目的,如作者信息標記有誤,請第一時間聯系我們修改或刪除,多謝。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 南康市| 什邡市| 晋城| 通城县| 镇康县| 同仁县| 额敏县| 浦北县| 乳山市| 板桥市| 巧家县| 乾安县| 山东省| 武陟县| 临漳县| 明水县| 寻甸| 自治县| 友谊县| 吴川市| 北海市| 陆河县| 玉门市| 南溪县| 黄浦区| 玉屏| 商洛市| 微山县| 离岛区| 嵊州市| 子洲县| 南昌市| 临洮县| 化德县| 平江县| 荔波县| 岳池县| 蓬莱市| 永顺县| 太仓市| 泾阳县|