php驗證碼_PHP教程

來源:互聯網
上載者:User
/**
* 驗證碼
* 2011/8/21
* kcj
* */
include "isLogin.php";
//隨機產生一個4位元字的驗證碼
$num='';
for($i=0;$i<4;$i++){
$num.=dechex(rand(0,20)); //dechex函數是十進位轉會二進位
}
session_start(); //開啟session
$_SESSION['yanzheng']=$num; //用session記住這個驗證數字
header("Content-type:image/PNG");
$im=imagecreate(60,20); //建立一個畫布
$back=imagecolorallocate($im,rand(0,55),rand(0,20),rand(0,5));//建立一個背景顏色 (黑色)
$gray=imagecolorallocate($im,rand(0,255),rand(0,200),rand(0,55)); //(白色)
imagefill($im,0,0,$gray); //填充顏色
$style=array($back,$back,$back,$back,$back,$gray,$gray,$gray,$gray,$gray); //產生數組
imagesetstyle($im,$style); //設定畫線風格
$y1=rand(0,20);
$y2=rand(0,20);
$y3=rand(0,20);
$y4=rand(0,20);
imageline($im,0,$y1,60,$y3,IMG_COLOR_STYLED); //畫一條線
imageline($im,0,$y2,60,$y4,IMG_COLOR_STYLED);
//在畫布上隨機產生大量黑點,起幹擾作用
for ($i=0;$i<80;$i++){
imagesetpixel($im,rand(0,60),rand(0,20),$back);
}
$str=rand(3,8);
for ($i=0;$i<4;$i++){
$strp=rand(1,6);
imagestring($im,6,$str,$strp,substr($num,$i,1),$back);
$str+=rand(8,12);
}
ImagePNG($im);
imagedestroy($im);

?>

摘自 chaojie2009的專欄

http://www.bkjia.com/PHPjc/478358.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/478358.htmlTechArticle? /** * 驗證碼 * 2011/8/21 * kcj * */ include isLogin.php; //隨機產生一個4位元字的驗證碼 $num=; for($i=0;$i4;$i++){ $num.=dechex(rand(0,20)); //dechex函數是十進位...

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.