這篇文章主要介紹了PHP製作驗證碼,使用PHP對網站驗證碼是為了防止使用程式惡意註冊、暴力破解或批量發帖而設定的。
網站註冊、登入又或者是留言頁面,都需要註冊碼來驗證當前操作者的合法性,為了防止網站被機器惡意註冊。
產生驗證碼無非就那麼幾個步驟,首先是擷取一個隨機字串,然後建立一個布畫,將產生的字串寫到布畫上,我們還可以在布畫上畫線畫雪花,現在帖一段產生驗證碼的代碼。
原始碼:
<?phpsession_start(); //開啟session//建立隨機碼,並儲存在session中for($i=0;$i<4;$i++){$_nmsg.=dechex(mt_rand(0,15));}//儲存到session中$_SESSION['code']=$_nmsg;//設定圖片長和高$_width=75;$_height=25;//建立一張映像$_img=imagecreatetruecolor($_width,$_height);//白色背景$_white=imagecolorallocate($_img,255,255,255);//填充到背景上imagefill($_img,0,0,$_white);//黑色邊框$_black=imagecolorallocate($_img,0,0,0);imagerectangle($_img,0,0,$_width-1,$_height-1,$_black);//隨即畫出5個線條for($i=0;$i<5;$i++){$_rnd_color=imagecolorallocate($_img,mt_rand(0,255),mt_rand(0,255),mt_rand(0,255));imageline($_img,mt_rand(0,$_width),mt_rand(0,$_height),mt_rand(0,$_width),mt_rand(0,$_height),$_rnd_color);}//雪花for($i=0;$i<10;$i++){$_rnd_color=imagecolorallocate($_img,mt_rand(200,255),mt_rand(200,255),mt_rand(200,255));imagestring($_img,1,mt_rand(1,$_width),mt_rand(1,$_height),"*",$_rnd_color);}//輸出驗證碼for($i=0;$i<strlen($_SESSION['code']);$i++){imagestring($_img,5,10+$i*15,mt_rand(0,10),$_SESSION['code'][$i],$_blackr);}//輸出映像header('Content-Type:image/png');imagepng($_img);//銷毀映像imagedestroy($_img);?>
代碼中將使用以下函數:
mt_rand — 產生更好的隨機數
int mt_rand ([ int $min ], int $max )很多老的 libc 的隨機數發生器具有一些不確定和未知的特性而且很慢。PHP 的 rand() 函數預設使用 libc 隨機數發生器。
mt_rand()函數是非正式用來替換它的。該函數用了Mersenne Twister中已知的特性作為隨機數發生器,它可以產生隨機數值的平均速度比 libc 提供的 rand() 快四倍。
dechex — 十進位轉換為十六進位返回一字串,包含有給定 number參數的十六進位表示。所能轉換的最大數值為十進位的 4294967295,其結果為 "ffffffff"。
imagecreatetruecolor — 建立一個真彩色映像
resource imagecreatetruecolor ( int $x_size , int $y_size )
imagecreatetruecolor() 返回一個映像標識符,代表了一幅大小為 x_size 和 y_size 的黑色映像。
imagecolorallocate — 為一幅映像分配顏色
int imagecolorallocate ( resource $image , int $red , int $green , int $blue )
imagecolorallocate() 返回一個標識符,代表了由給定的 RGB 成分組成的顏色。red,green 和 blue 分別是所需要的顏色的紅,綠,藍成分。這些參數是 0 到 255 的整數或者十六進位的 0x00 到 0xFF。imagecolorallocate()必須被調用以建立每一種用在 image 所代表的映像中的顏色。
imagefill — 地區填充
bool imagefill ( resource $image , int $x , int $y , int $color )
imagefill() 在 image映像的座標 x,y(映像左上方為 0, 0)處用 color顏色執列區域填充(即與 x, y 點顏色相同且相鄰的點都會被填充)。
imagerectangle — 畫一個矩形
bool imagerectangle ( resource $image , int $x1 , int $y1 , int $x2 , int $y2 , int $col )
imagerectangle() 用 col 顏色在 image 映像中畫一個矩形,其左上方座標為 x1, y1,右下角座標為 x2, y2。映像的左上方座標為 0, 0。
imageline — 畫一條線段
bool imageline ( resource $image , int $x1 , int $y1 , int $x2 , int $y2 , int $color )
imageline() 用 color顏色在映像 image 中從座標 x1,y1 到 x2,y2(映像左上方為 0, 0)畫一條線段。
imagestring — 水平地畫一行字串
bool imagestring ( resource $image , int $font , int $x , int $y , string $s , int $col )
imagestring() 用 col顏色將字串 s 畫到 image所代表的映像的 x,y座標處(這是字串左上方座標,整幅映像的左上方為 0,0)。如果 font 是 1,2,3,4 或 5,則使用內建字型。
imagepng — 以 PNG 格式將映像輸出到瀏覽器或檔案
imagepng() 將 GD 映像流(image)以 PNG 格式輸出到標準輸出(通常為瀏覽器),或者如果用 filename 給出了檔案名稱則將其輸出到該檔案。
imagedestroy — 銷毀一映像
imagedestroy() 釋放與 image 關聯的記憶體。
將原始碼儲存為code.php是個php檔案,我們該如何使用他呢?
imagepng已經將這個php檔案輸出成了png檔案
直接調用就可以了
<img src="mycode.php"/>
如果要使用驗證碼,記得開啟session哦
<?phpsession_start();echo $_SESSION['code'];?>
總結:以上就是本篇文的全部內容,希望能對大家的學習有所協助。