產生驗證碼 隨機幹擾php實現

來源:互聯網
上載者:User
  1. session_start ();
  2. header ( 'Content-type: image/png' );
  3. //建立圖片
  4. $im = imagecreate($x=130,$y=45 );
  5. $bg = imagecolorallocate($im,rand(50,200),rand(0,155),rand(0,155)); //第一次對 imagecolorallocate() 的調用會給基於調色盤的映像填充背景色
  6. $fontColor = imageColorAllocate ( $im, 255, 255, 255 ); //字型顏色
  7. $fontstyle = 'rock.ttf'; //字型樣式,這個可以從c:\windows\Fonts\檔案夾下找到,我把它放到和authcode.php檔案同一個目錄,這裡可以替換其他的字型樣式
  8. //產生隨機字元
  9. for($i = 0; $i < 4; $i ++) {
  10. $randAsciiNumArray = array (rand(48,57),rand(65,90));
  11. $randAsciiNum = $randAsciiNumArray [rand ( 0, 1 )];
  12. $randStr = chr ( $randAsciiNum );
  13. imagettftext($im,30,rand(0,20)-rand(0,25),5+$i*30,rand(30,35),$fontColor,$fontstyle,$randStr);
  14. $authcode .= $randStr;
  15. }
  16. $_SESSION['authcode'] = $randFourStr;//使用者和使用者輸入的驗證碼做比較
  17. //幹擾線
  18. for ($i=0;$i<8;$i++){
  19. $lineColor = imagecolorallocate($im,rand(0,255),rand(0,255),rand(0,255));
  20. imageline ($im,rand(0,$x),0,rand(0,$x),$y,$lineColor);
  21. }
  22. //幹擾點
  23. for ($i=0;$i<250;$i++){
  24. imagesetpixel($im,rand(0,$x),rand(0,$y),$fontColor);
  25. }
  26. imagepng($im);
  27. imagedestroy($im);
  28. ?>
複製代碼
驗證碼, php
  • 聯繫我們

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