php產生驗證碼代碼_PHP教程

來源:互聯網
上載者:User
本款php產生驗證碼代碼原理產生隨機數-->建立圖片-->隨機數寫進圖片-->儲存在session中 ,看一下流程驗證碼圖片產生 通知瀏覽器將要輸出PNG圖片 準備好隨機數發生器種子 srand((double)microtime()*1000000); 將四位整數驗證碼繪入圖片

/php教程產生驗證碼代碼
/*=====================================================
本款php產生驗證碼代碼原理產生隨機數-->建立圖片-->隨機數寫進圖片-->儲存在session中 ,看一下流程驗證碼圖片產生 通知瀏覽器將要輸出png圖片 準備好隨機數發生器種子 srand((double)microtime()*1000000); 將四位整數驗證碼繪入圖片
=======================================================*/

/*=====================
產生隨機字串函數
=====================*/
function random($length) {
$hash = '';
$chars = 'abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz';
$max = strlen($chars) - 1;
mt_srand((double)microtime() * 1000000);
for($i = 0; $i < $length; $i++) {
$hash .= $chars[mt_rand(0, $max)];
}
return $hash;
}

//驗證碼圖片產生

session_start();
//通知瀏覽器將要輸出png圖片
header("content-type: image/png");
//準備好隨機數發生器種子
//srand((double)microtime()*1000000);
//準備圖片的相關參數
$im = imagecreate(62,22);
$black = imagecolorallocate($im, 0,0,0); //rgb黑色標識符
$white = imagecolorallocate($im, 255,255,255); //rgb白色標識符
$gray = imagecolorallocate($im, 179,183,185); //rgb灰色標識符
//開始作圖
imagefill($im,0,0,$gray);
//while(($randval=rand()%100000)<10000);{
//$_session["check_code"] = $randval;
//將四位整數驗證碼繪入圖片
$randval=random(4);
$_session["check_code"]=$randval;
imagestring($im, 5, 10, 3, $randval, $white);
//}
//加入幹擾象素
for($i=0;$i<150;$i++){
$randcolor = imagecolorallocate($im,rand(0,255),rand(0,255),rand(0,255));
imagesetpixel($im, rand()%70 , rand()%30 , $white);
}
//輸出驗證圖片
imagepng($im);
//銷毀映像標識符
imagedestroy($im);

?>

http://www.bkjia.com/PHPjc/633022.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/633022.htmlTechArticle本款php產生驗證碼代碼原理產生隨機數-->建立圖片-->隨機數寫進圖片-->儲存在session中 ,看一下流程驗證碼圖片產生 通知瀏覽器將要輸出P...

  • 聯繫我們

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