PHP驗證碼的製作

2022-03-31 20:05:52 字數 1087 閱讀 8398

<?php 

if(!isset($_session))

/**隨機生成驗證碼函式

*param $m:驗證碼位數 預設值為4

*param $type:驗證碼型別 0:數學 1:數字+小寫字母 2:數字+大小寫字母

*/function getcode($m=4,$type=0)

return$c;

}//1.獲取驗證碼字元

$num=4;

$str = getcode($num,0);

$_session['verifycode'] = $str;//

2.建立畫布

$width=$num*20;

$height=30;

$im = imagecreatetruecolor($width,$height

);$c = imagecolorallocate($im,111,0,55);

$rect = imagecolorallocate($im,20,0,190);

$bg = imagecolorallocate($im,200,200,200);

//3.

開始繪製

imagefill($im,0,0,$bg

);//

背景色imagerectangle(

$im,0,0,$width-1,$height-1,$rect

);//邊框

//4.

新增干擾點

for($i=0; $i

<200; $i++)

//5.

新增干擾線

for($i=0; $i

<5; $i++)

//6.

繪製驗證碼內容

for($i=0; $i

<$num; $i++)

//7.輸出驗證碼圖形

header("content-type:image/png");

imagepng(

$im);

imagedestroy(

$im);

?>

這裡只是php驗證碼的製作部分,關於驗證碼的使用還需根據具體的情況而定。

php 驗證碼製作

直接上 function buildrandomstring type 1,length 4 else if type 2 else if type 3 if length strlen chars 隨意打亂字串 chars str shuffle chars return substr chars...

php製作驗證碼

session start 型別 type gif 的尺寸 width 40 height 16 header content type image type srand double microtime 1000000 生成字元的個數 randval randstr 4,if type gif f...

php製作驗證碼

用php製作乙個一串驗證碼很簡單,主要需要用到一下函式,到手冊上查詢下就知道了,需要注意imagettftext,需要乙個字型檔案,我在win7控制面板字型裡拷貝出來的.這個函式比imagestring 好一點是因為,可以改變字型,和字型大小,imagestring只提供內建的字型大小1,2,3,4...