使用php製作驗證碼技術

2021-06-28 13:52:45 字數 1334 閱讀 5104

這個函式是用來產生驗證碼所需的字串。原理,首先把字母數字整合到一起,然後使用str_shuffle函式將字串打亂,然後擷取所需要的長度返回。

<?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,0,$length);

}用來製作驗證碼的部分

<?php

//製作畫布

require_once 'string.func.php';

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

$width=180;

$height=28;

//設定顏色

$image=imagecreatetruecolor($width, $height);

$white=imagecolorallocate($image,255,255,255);

$black=imagecolorallocate($image,0,0,0);

imagefilledrectangle($image,1,1,$width-2,$height-2,$black);

//imagefill($image,0,0,$white);

//imageline($image,10,10,90,40,$black);

//型別是1,長度是8

$type=1;

$length=8;

//根據型別和長度設定驗證碼

$chars=buildrandomstring($type,$length);

$sess_name="verify";

$_session[$sess_name]=$chars;

//製作驗證碼需要的字型檔案

$fontfiles=array("msyh.ttf","msyhbd.ttf","simfang.ttf","simhei.ttf","simkai.ttf","simsun.ttc");

for($i=0;$i<$length;$i++)

imagepng($image);

//記得釋放資源

imagedestroy($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...