PHP 驗證碼製作

2021-08-05 18:36:20 字數 3841 閱讀 2976

session儲存驗證資訊

漢字驗證碼實現

實現**

驗證碼

1.在製作驗證碼之前請確認php的gd庫(影象處理庫)是否開啟。(phpinfo()檢視)

2.在輸出影象之前,一定要在先設定header('content-type: image/png')

3.在給驗證碼字元顏色取值的時候一般r、g、b的取值在0-120之間(深色區間)(值越大顏色越淺)

4.再出現顯示錯誤無法顯示的情況下,請先遮蔽header和輸出的語句,然後執行檢查自己是否有用echo函式輸出其他內容,或者程式報錯。

$width:寬

$height:高

說明:根據影象來構建顏色,對於用 imagecreate() 建立的影象,第一次呼叫 imagecolorallocate() 會自動給影象填充背景色

$image:資源控制代碼

$red:紅色比例(0-255)

$green:綠色比例(0-255)

$blue:藍色比例(0-255)

返回值:返回顏色值

說明:為影象填充顏色(x,y 分別為填充的起始 x 座標和 y 座標,與 x, y 點顏色相同且相鄰的點都會被填充。 )

$image:資源控制代碼

$x:x座標

$y:y座標

$color:指定的顏色

$image:資源控制代碼

$image:資源控制代碼

說明:在影象上繪出文字,不支援中文

$image:資源控制代碼

$font:字型型別(如果是數字就使用內建字型,數字代表大小)

$x:x座標

$y:y座標

$string:要繪出的文字

$color:文字的顏色

說明:在影象指定點上繪製指定顏色

$image:資源控制代碼

$x:x座標

$y:y座標

$color:指定的顏色

說明:在影象上繪製線段

$image:資源控制代碼

$x1:起點x1座標

$y1:起點y1座標

$x2:終點x2座標

$y2:終點y2座標

$color:指定的顏色

1.必須在指令碼最頂部呼叫session_start()函式

2.多伺服器情況,需要考慮集中管理session資訊

3.用$_session全域性變數來儲存驗證資訊,下次直接讀就行了。

array imagettftext ( resource im

age,

floa

t size , float an

gle,

int x , int y,

int color , string fo

ntfi

le,s

trin

g text )

說明:使用 truetype 字型將 指定的 text 寫入影象,可以調整角度。
這裡用中文驗證碼做示例,可以自己更改生成驗證碼的位址

所有生成驗證碼的函式都會自動輸出驗證碼並且根據你提供的session_key儲存到$_session[session_key]中,並在最後返回他繪製在驗證碼上的文字。

define("pic_num_point",200);//干擾點數量

define("pic_num_line",6);//干擾線數量

getchinacaptcha("captcha");

//-獲取中文驗證碼

function getchinacaptcha($session_key)

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

imagepng($image);

$_session[$session_key] = $captcha;

return

$captcha;

} function setbackgroundpoint($image)

}function setbackgroundline($image)

} ?>

<?php

session_start();

define("pic_width", 100);

define("pic_height", 50);

define("pic_num_word",4);

define("pic_num_point",200);//干擾點數量

define("pic_num_line",6);//干擾線數量

getnumbercaptcha("captcha");

//-獲取數字驗證碼

function getnumbercaptcha($session_key)

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

imagepng($image);

$_session[$session_key] = $captcha;

return

$captcha;

}function setbackgroundpoint($image)

}function setbackgroundline($image)

}

<?php

session_start();

define("pic_width", 100);

define("pic_height", 50);

define("pic_num_word",4);

define("pic_num_point",200);//干擾點數量

define("pic_num_line",6);//干擾線數量

getnumberwordcaptcha("captcha");

//-獲取數字與字母混合驗證碼

function getnumberwordcaptcha($session_key)

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

imagepng($image);

$_session[$session_key] = $captcha;

return

$captcha;

}function setbackgroundpoint($image)

}function setbackgroundline($image)

}

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...