導(dǎo)航首頁(yè) ? 技術(shù)教程 ? 基于PHP制作驗(yàn)證碼
    全站頭部文字 我要出現(xiàn)在這里
    基于PHP制作驗(yàn)證碼 530 2024-01-24   

    網(wǎng)站注冊(cè)、登錄又或者是留言頁(yè)面,都需要注冊(cè)碼來(lái)驗(yàn)證當(dāng)前操作者的合法性,為了防止網(wǎng)站被機(jī)器惡意注冊(cè)。

    生成驗(yàn)證碼無(wú)非就那么幾個(gè)步驟,首先是獲取一個(gè)隨機(jī)字符串,然后創(chuàng)建一個(gè)布畫(huà),將生成的字符串寫(xiě)到布畫(huà)上,我們還可以在布畫(huà)上畫(huà)線(xiàn)畫(huà)雪花,現(xiàn)在帖一段生成驗(yàn)證碼的代碼。

    源代碼:

    <?php
    session_start(); //開(kāi)啟session
    //創(chuàng)建隨機(jī)碼,并保存在session中
    for($i=0;$i<4;$i++)
    {
    $_nmsg.=dechex(mt_rand(0,15));
    }
    //保存到session中
    $_SESSION['code']=$_nmsg;
    //設(shè)置圖片長(zhǎng)和高
    
    $_width=75;
    $_height=25;
    //創(chuàng)建一張圖像
    $_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);
    
    //隨即畫(huà)出5個(gè)線(xiàn)條
    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);
    }
    
    //輸出驗(yàn)證碼
    
    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);
    //銷(xiāo)毀圖像
    imagedestroy($_img);
    ?>
    
    

    代碼中將使用以下函數(shù):

    mt_rand — 生成更好的隨機(jī)數(shù)
    int mt_rand ([ int $min ], int $max )很多老的 libc 的隨機(jī)數(shù)發(fā)生器具有一些不確定和未知的特性而且很慢。PHP 的 rand() 函數(shù)默認(rèn)使用 libc 隨機(jī)數(shù)發(fā)生器。

    mt_rand()函數(shù)是非正式用來(lái)替換它的。該函數(shù)用了Mersenne Twister中已知的特性作為隨機(jī)數(shù)發(fā)生器,它可以產(chǎn)生隨機(jī)數(shù)值的平均速度比 libc 提供的 rand() 快四倍。

    dechex — 十進(jìn)制轉(zhuǎn)換為十六進(jìn)制返回一字符串,包含有給定 number參數(shù)的十六進(jìn)制表示。所能轉(zhuǎn)換的最大數(shù)值為十進(jìn)制的 4294967295,其結(jié)果為 "ffffffff"。

    imagecreatetruecolor — 新建一個(gè)真彩色圖像
    resource imagecreatetruecolor ( int $x_size , int $y_size )

    imagecreatetruecolor() 返回一個(gè)圖像標(biāo)識(shí)符,代表了一幅大小為 x_size 和 y_size 的黑色圖像。

    imagecolorallocate — 為一幅圖像分配顏色
    int imagecolorallocate ( resource $image , int $red , int $green , int $blue )
    imagecolorallocate() 返回一個(gè)標(biāo)識(shí)符,代表了由給定的 RGB 成分組成的顏色。red,green 和 blue 分別是所需要的顏色的紅,綠,藍(lán)成分。這些參數(shù)是 0 到 255 的整數(shù)或者十六進(jìn)制的 0x00 到 0xFF。imagecolorallocate()必須被調(diào)用以創(chuàng)建每一種用在 image 所代表的圖像中的顏色。

    imagefill — 區(qū)域填充
    bool imagefill ( resource $image , int $x , int $y , int $color )
    imagefill() 在 image圖像的坐標(biāo) x,y(圖像左上角為 0, 0)處用 color顏色執(zhí)行區(qū)域填充(即與 x, y 點(diǎn)顏色相同且相鄰的點(diǎn)都會(huì)被填充)。

    imagerectangle — 畫(huà)一個(gè)矩形
    bool imagerectangle ( resource $image , int $x1 , int $y1 , int $x2 , int $y2 , int $col )
    imagerectangle() 用 col 顏色在 image 圖像中畫(huà)一個(gè)矩形,其左上角坐標(biāo)為 x1, y1,右下角坐標(biāo)為 x2, y2。圖像的左上角坐標(biāo)為 0, 0。

    imageline — 畫(huà)一條線(xiàn)段
    bool imageline ( resource $image , int $x1 , int $y1 , int $x2 , int $y2 , int $color )
    imageline() 用 color顏色在圖像 image 中從坐標(biāo) x1,y1 到 x2,y2(圖像左上角為 0, 0)畫(huà)一條線(xiàn)段。

    imagestring — 水平地畫(huà)一行字符串
    bool imagestring ( resource $image , int $font , int $x , int $y , string $s , int $col )
    imagestring() 用 col顏色將字符串 s 畫(huà)到 image所代表的圖像的 x,y坐標(biāo)處(這是字符串左上角坐標(biāo),整幅圖像的左上角為 0,0)。如果 font 是 1,2,3,4 或 5,則使用內(nèi)置字體。

    imagepng — 以 PNG 格式將圖像輸出到瀏覽器或文件
    imagepng() 將 GD 圖像流(image)以 PNG 格式輸出到標(biāo)準(zhǔn)輸出(通常為瀏覽器),或者如果用 filename 給出了文件名則將其輸出到該文件。

    imagedestroy — 銷(xiāo)毀一圖像

    imagedestroy() 釋放與 image 關(guān)聯(lián)的內(nèi)存。

    將源代碼保存為code.php是個(gè)php文件,我們?cè)撊绾问褂盟兀?br />

    imagepng已經(jīng)將這個(gè)php文件輸出成了png文件

    直接調(diào)用就可以了

    <img src="http://www.gimoo.net/t/1810/mycode.php"/>

    如果要使用驗(yàn)證碼,記得開(kāi)啟session哦

    <?php
    session_start();
    echo $_SESSION['code'];
    ?>

    希望本文所述對(duì)大家PHP程序設(shè)計(jì)有所幫助。


    PHP

    !!!站長(zhǎng)長(zhǎng)期在線(xiàn)接!!!

    網(wǎng)站、小程序:定制開(kāi)發(fā)/二次開(kāi)發(fā)/仿制開(kāi)發(fā)等

    各種疑難雜癥解決/定制接口/定制采集等

    站長(zhǎng)微信:lxwl520520

    站長(zhǎng)QQ:1737366103