剛寫了一個PHP的高效驗證碼函數

來源:互聯網
上載者:User
我不喜歡因為一個簡單的驗證碼就大費周章,甚至連font字型檔檔案都有,常常發現有的程式不過才兩兆多點,而字型檔佔去了至少1.5兆,唉!搞不懂那位是怎麼想的,簡直就是太有創意了
我不喜歡,所以自己寫了一個簡單而又漂亮的高效的.

//如果要轉載本文請註明出處,免的出現版權紛爭,我不喜歡看到那種轉載了我的作品卻不註明出處的人 Seven{See7di#Gmail.com}

<?php
$work=AddsLashes($_REQUEST["work"]);
Switch ($work){
Case "cod":
_cod();
Break;
Default:
_main();
Break;
}
//------------------------------------------------*
Function _main(){
Session_start();
Echo "<img src='?work=cod' width='68' height='20' onclick=\"this.src='?work=cod&'+Math.random(10)\" style='cursor:pointer;'>".$_SESSION['cod'];
}
//------------------------------------------------*
Function _cod(){
OB_start();
OB_clean();
For($i=0;$i<4;$i++){//dechex將10進位轉為16進位
$num .= Dechex(Mt_Rand(1,15));
}

Session_start();$_SESSION['cod']=$num;

$img=Imagecreatetruecolor(60,20);//建立畫布
$bgc=Imagecolorallocate($img,255,255,255);//建立圖片背景顏色
Imagefill($img,0,0,$bgc);

For($i=0;$i<Mt_Rand(0,5);$i++){//添加隨機線條
$linc=Imagecolorallocate($img,200,200,Mt_Rand(0,255));
Imageline($img,Mt_Rand(5,55),Mt_Rand(5,15),Mt_Rand(5,55),Mt_Rand(5,15),$linc);
}

For($i=0;$i<2;$i++){
$col=Imagecolorallocate($img,Mt_Rand(10,60),Mt_Rand(100,150),Mt_Rand(0,200));//字型顏色
Imagestring($img,6,(10+$i*9),(2+$i*2),($num[$i]."-".$num[($i+2)]),$col);//寫入文字
}

Header('content-type:Image/png');
Imagepng($img);
Die();
}
?>

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.