溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

php生成驗(yàn)證碼需要注意的

發(fā)布時(shí)間:2020-08-02 19:21:06 來源:網(wǎng)絡(luò) 閱讀:382 作者:huangyanxiong 欄目:web開發(fā)

<?php
    session_start();
    $gd=gd_info();
    if (!$gd['FreeType Support']){
        exit('該服務(wù)不支持gdlib');
    }
    //文件頭...
    header("Content-type: p_w_picpath/png");
    //創(chuàng)建真彩色白紙
    $im = @p_w_picpathcreatetruecolor(100, 40) or die("建立圖像失敗");
    //獲取背景顏色
    $background_color = p_w_picpathcolorallocate($im, 250, 205, 205);
    //填充背景顏色(這個(gè)東西類似油桶)
    p_w_picpathfill($im,0,0,$background_color);
    //獲取邊框顏色
    $border_color = p_w_picpathcolorallocate($im,200,200,200);
    //畫矩形,邊框顏色200,200,200
    p_w_picpathrectangle($im,0,0,100,40,$border_color);
    //逐行炫耀背景,全屏用1或0
    for($i=2;$i<40;$i++){
        //獲取隨機(jī)淡色     
        $line_color = p_w_picpathcolorallocate($im,rand(200,255),rand(200,255),rand(200,255));
        //畫線
        p_w_picpathline($im,0,$i,100,$i,$line_color);
    }
    //設(shè)置字體大小
    $font_size=28;
    //設(shè)置印上去的文字
    $Str[0] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    $Str[1] = "abcdefghijklmnopqrstuvwxyz";
    $Str[2] = "01234567891234567890123456";
    //獲取第1個(gè)隨機(jī)文字
    $imstr[0]["s"] = $Str[rand(0,2)][rand(0,25)];
    $imstr[0]["x"] = rand(2,5);
    $imstr[0]["y"] = rand(1,4);
    //獲取第2個(gè)隨機(jī)文字
    $imstr[1]["s"] = $Str[rand(0,2)][rand(0,25)];
    $imstr[1]["x"] = $imstr[0]["x"]+$font_size-1+rand(0,1);
    $imstr[1]["y"] = rand(1,3);
    //獲取第3個(gè)隨機(jī)文字
    $imstr[2]["s"] = $Str[rand(0,2)][rand(0,25)];
    $imstr[2]["x"] = $imstr[1]["x"]+$font_size-1+rand(0,1);
    $imstr[2]["y"] = rand(1,4);
    //獲取第4個(gè)隨機(jī)文字
    $imstr[3]["s"] = $Str[rand(0,2)][rand(0,25)];
    $imstr[3]["x"] = $imstr[2]["x"]+$font_size-1+rand(0,1);
    $imstr[3]["y"] = rand(1,3);
    //寫入隨機(jī)字串
    for($i=0;$i<4;$i++){
        //獲取隨機(jī)較深顏色
        $text_color = p_w_picpathcolorallocate($im,rand(50,180),rand(50,180),rand(50,180));
        //畫文字
        p_w_picpathstring($im,$font_size,$imstr[$i]["x"],rand(0, 20),$imstr[$i]["s"],$text_color);
        //p_w_picpathttftext($im, $font_size, 0 , $imstr[$i]["x"], rand(0, 20), $text_color, font.ttf, $imstr[$i]["s"]);
$captcha=$captcha.$imstr[$i]["s"];  //只要這里進(jìn)行字符串連接時(shí),驗(yàn)證碼就不能顯示
    }
    $_SESSION['captcha']=$captcha;
    //顯示圖片
    p_w_picpathpng($im);
    //銷毀圖片
   p_w_picpathdestroy($im);
?

1:生成圖像的php頁面不能有含有一些沒有初始化的變量

原因:這些沒有初始化的變量會(huì)出現(xiàn)一個(gè)notice提示,輸出到圖片流中,打亂圖片流的代碼,導(dǎo)致驗(yàn)證碼不能顯示,這就好比session_start()前不能有任何的輸出,但是這個(gè)notice提示是很少人能夠看得出來的,在這個(gè)例子中的$captcha就是沒有初始化的,需要在使用這個(gè)變量前要初始化,就不會(huì)有問題了



向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI