php實現(xiàn)的數(shù)字驗證碼及數(shù)字運(yùn)算驗證碼
更新時間:2015年07月30日 10:08:22 作者:路人甲123
這篇文章主要介紹了php實現(xiàn)的數(shù)字驗證碼及數(shù)字運(yùn)算驗證碼,以實例形式分別描述了php實現(xiàn)數(shù)字驗證碼及數(shù)學(xué)運(yùn)算驗證碼的相關(guān)技巧,非常簡單實用,需要的朋友可以參考下
本文實例講述了php實現(xiàn)的數(shù)字驗證碼及數(shù)字運(yùn)算驗證碼。分享給大家供大家參考。具體如下:
1. 數(shù)字驗證碼:
<?php
//第一個實例是數(shù)字驗證碼,最常見的驗證碼。多少個數(shù)字可以由自己決定。
//$num是生成的驗證碼包含幾個數(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ù)字驗證碼,可以用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ū)域填充
//給驗證碼畫上邊框,感覺驗證碼美美的,其實呢畫邊框就是給他畫上一個矩形
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),保證不會重疊??v坐標(biāo)不管
}
//substr() 函數(shù)返回字符串的一部分,substr(string,start,length),要截取的字符串,start開始位置,length截取的長度
//生成好了數(shù)字,下面要給驗證碼區(qū)域一些干擾,防止一些工具可以自動識別
//1.方法一:給背景加上很多噪點
//imagesetpixel($image,$x,$y,$color),在image圖像中用color顏色在x,y坐標(biāo)(圖像左上角為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),設(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)算驗證碼:
<?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è)計有所幫助。
您可能感興趣的文章:
- php 判斷是否是中文/英文/數(shù)字示例代碼
- PHP的幾個常用數(shù)字判斷函數(shù)代碼
- php判斷輸入是否是純數(shù)字,英文,漢字的方法
- php中數(shù)字、字符與對象判斷函數(shù)用法實例
- 利用PHP判斷是否是連乘數(shù)字串的方法示例
- PHP正則匹配中英文、數(shù)字及下劃線的方法【用戶名驗證】
- PHP寫的簡單數(shù)字驗證碼實例
- php生成4位數(shù)字驗證碼的實現(xiàn)代碼
- php數(shù)字運(yùn)算驗證碼的實現(xiàn)代碼
- php生成數(shù)字字母的驗證碼圖片
- 支持中文、字母、數(shù)字的PHP驗證碼
- 支持中文字母數(shù)字、自定義字體php驗證碼代碼
- php中文字母數(shù)字驗證碼實現(xiàn)代碼
- php用正則判斷是否為數(shù)字的方法
相關(guān)文章
php Smarty date_format [格式化時間日期]
php Smarty date_format [格式化時間日期] ,需要的朋友可以參考下。2010-03-03
PHP創(chuàng)建XML的方法示例【基于DOMDocument類及SimpleXMLElement類】
這篇文章主要介紹了PHP創(chuàng)建XML的方法,結(jié)合實例形式分析了php基于DOMDocument類及SimpleXMLElement類創(chuàng)建xml文件的具體步驟與相關(guān)實現(xiàn)技巧,需要的朋友可以參考下2019-09-09
PHP的Yii框架中過濾器相關(guān)的使用總結(jié)
這篇文章主要介紹了PHP的Yii框架中過濾器相關(guān)的使用總結(jié),文中列舉了一些常用的核心過濾器,需要的朋友可以參考下2016-03-03
php中&&和||邏輯運(yùn)算符的高級簡寫(縮寫條件)用法由淺入深講解
php中if進(jìn)行多條件判斷時,使用邏輯運(yùn)算符&&和||(and和or),這樣的寫法很常見也很熟悉。&&和||還有高級簡寫(縮寫條件)用法,比如單獨(dú)一行中“條件A||條件B”進(jìn)行了什么操作?本文將由淺入深詳細(xì)講解php中&&和||邏輯運(yùn)算符的高級簡寫(縮寫條件)用法。2022-11-11

