《PHP教程:基于PHP制作驗證碼》要點:
本文介紹了PHP教程:基于PHP制作驗證碼,希望對您有用。如果有疑問,可以聯系我們。
PHP實戰網站注冊、登錄又或者是留言頁面,都需要注冊碼來驗證當前操作者的合法性,為了防止網站被機器惡意注冊.
PHP實戰生成驗證碼無非就那么幾個步驟,首先是獲取一個隨機字符串,然后創建一個布畫,將生成的字符串寫到布畫上,我們還可以在布畫上畫線畫雪花,現在帖一段生成驗證碼的代碼.
PHP實戰源代碼:
PHP實戰
<?php
session_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);
?>
PHP實戰代碼中將使用以下函數:
PHP實戰mt_rand ― 生成更好的隨機數
int mt_rand ([ int $min ], int $max )很多老的 libc 的隨機數發生器具有一些不確定和未知的特性而且很慢.PHP 的 rand() 函數默認使用 libc 隨機數發生器.
PHP實戰mt_rand()函數是非正式用來替換它的.該函數用了Mersenne Twister中已知的特性作為隨機數發生器,它可以產生隨機數值的平均速度比 libc 提供的 rand() 快四倍.
PHP實戰dechex ― 十進制轉換為十六進制返回一字符串,包含有給定 number參數的十六進制表示.所能轉換的最大數值為十進制的 4294967295,其結果為 "ffffffff".
PHP實戰imagecreatetruecolor ― 新建一個真彩色圖像
resource imagecreatetruecolor ( int $x_size , int $y_size )
PHP實戰imagecreatetruecolor() 返回一個圖像標識符,代表了一幅大小為 x_size 和 y_size 的黑色圖像.
PHP實戰imagecolorallocate ― 為一幅圖像分配顏色
int imagecolorallocate ( resource $image , int $red , int $green , int $blue )
imagecolorallocate() 返回一個標識符,代表了由給定的 RGB 成分組成的顏色.red,green 和 blue 分別是所需要的顏色的紅,綠,藍成分.這些參數是 0 到 255 的整數或者十六進制的 0x00 到 0xFF.imagecolorallocate()必須被調用以創建每一種用在 image 所代表的圖像中的顏色.
PHP實戰imagefill ― 區域填充
bool imagefill ( resource $image , int $x , int $y , int $color )
imagefill() 在 image圖像的坐標 x,y(圖像左上角為 0, 0)處用 color顏色執行區域填充(即與 x, y 點顏色相同且相鄰的點都會被填充).
PHP實戰imagerectangle ― 畫一個矩形
bool imagerectangle ( resource $image , int $x1 , int $y1 , int $x2 , int $y2 , int $col )
imagerectangle() 用 col 顏色在 image 圖像中畫一個矩形,其左上角坐標為 x1, y1,右下角坐標為 x2, y2.圖像的左上角坐標為 0, 0.
PHP實戰imageline ― 畫一條線段
bool imageline ( resource $image , int $x1 , int $y1 , int $x2 , int $y2 , int $color )
imageline() 用 color顏色在圖像 image 中從坐標 x1,y1 到 x2,y2(圖像左上角為 0, 0)畫一條線段.
PHP實戰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,則使用內置字體.
PHP實戰imagepng ― 以 PNG 格式將圖像輸出到瀏覽器或文件
imagepng() 將 GD 圖像流(image)以 PNG 格式輸出到標準輸出(通常為瀏覽器),或者如果用 filename 給出了文件名則將其輸出到該文件.
PHP實戰imagedestroy ― 銷毀一圖像
PHP實戰imagedestroy() 釋放與 image 關聯的內存.
PHP實戰將源代碼保存為code.php是個php文件,我們該如何使用他呢?
PHP實戰imagepng已經將這個php文件輸出成了png文件
PHP實戰直接調用就可以了
PHP實戰<img src="mycode.php"/>
PHP實戰如果要使用驗證碼,記得開啟session哦
PHP實戰
<?php
session_start();
echo $_SESSION['code'];
?>
PHP實戰希望本文所述對大家PHP程序設計有所幫助.
轉載請注明本頁網址:
http://www.snjht.com/jiaocheng/3074.html