本文實(shí)例講述了php實(shí)現(xiàn)的數(shù)字驗(yàn)證碼及數(shù)字運(yùn)算驗(yàn)證碼。分享給大家供大家參考。具體如下:
1. 數(shù)字驗(yàn)證碼:
<?php
//第一個實(shí)例是數(shù)字驗(yàn)證碼,最常見的驗(yàn)證碼。多少個數(shù)字可以由自己決定。
//$num是生成的驗(yàn)證碼包含幾個數(shù)字
getValidate(4,60,20);
function getValidate($num,$w,$h){
$code = "";
for($i=0;$i<$num;$i++){
$code .= rand(0,9);
}
//code變量最后是$num個數(shù)字,并且是字符串。因襲如果是生成四位的數(shù)字驗(yàn)證碼,可以用rand(1000,9999)生成
Header("Content-type:image/PNG");
$img = imagecreate($w,$h);//創(chuàng)建圖片,長寬參數(shù)是一開始定義好的。
$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圖像的坐標(biāo)x,y處用color顏色執(zhí)行區(qū)域填充
//給驗(yàn)證碼畫上邊框,感覺驗(yàn)證碼美美的,其實(shí)呢畫邊框就是給他畫上一個矩形
imagerectangle($img,0,0,$w-1,$h-1,$black);//用黑色的框框
/*imagestring($image,$font,$x,$y,$s,$col)
用col顏色將字符串s畫到image所代表的圖像的x,y處(xy是字符串的左上角坐標(biāo)),
整幅圖像的左上角為(0,0)如果font是1,2,3,4,5則使用內(nèi)置字體*/
//一下是畫四個數(shù)字的方法,讓四個字符位置都隨機(jī)等
$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是橫坐標(biāo),需要每一次加上之前的坐標(biāo),保證不會重疊。縱坐標(biāo)不管
}
//substr() 函數(shù)返回字符串的一部分,substr(string,start,length),要截取的字符串,start開始位置,length截取的長度
//生成好了數(shù)字,下面要給驗(yàn)證碼區(qū)域一些干擾,防止一些工具可以自動識別
//1.方法一:給背景加上很多噪點(diǎn)
//imagesetpixel($image,$x,$y,$color),在image圖像中用color顏色在x,y坐標(biāo)(圖像左上角為0,0)上畫一個點(diǎn)
for($i = 0;$i<80;$i++){
imagesetpixel($img, rand(0,$w), rand(0,$h), $black);//這些點(diǎn)分布在這個背景里面
}
//2.方法二:繪制幾條虛線
$style = array($black,$gray);
imagesetstyle($img, $style);
//imagesetstyle($image,$style),設(shè)定畫線的風(fēng)格,像素組成的數(shù)組
$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. 數(shù)字運(yùn)算驗(yàn)證碼:
<?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程序設(shè)計(jì)有所幫助。
新聞熱點(diǎn)
疑難解答
圖片精選