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

首頁 > 網站 > 建站經驗 > 正文

php實現的數字驗證碼及數字運算驗證碼

2024-04-25 20:40:25
字體:
來源:轉載
供稿:網友

本文實例講述了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. 數字運算驗證碼:

<?php

getValidate(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程序設計有所幫助。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 武宁县| 南投县| 彩票| 精河县| 平和县| 汕尾市| 高邮市| 柯坪县| 巴南区| 武山县| 枣庄市| 惠州市| 云安县| 郯城县| 宝应县| 巩留县| 通海县| 郁南县| 延边| 江达县| 峡江县| 鄯善县| 吉林省| 大英县| 水城县| 无为县| 三门峡市| 葫芦岛市| 泌阳县| 鄢陵县| 芦山县| 理塘县| 崇义县| 平定县| 大余县| 贡觉县| 韩城市| 阜平县| 明光市| 深圳市| 隆德县|