php驗證碼實現代碼

來源:互聯網
上載者:User
PHP驗證碼實現原理

產生隨機數或者字母儲存到session中(驗證驗證碼的時候用),然後對產生的數字或者字母進行繪圖!然後呈現在我們眼前

重新整理驗證碼:用js改變驗證碼圖片所帶的參數,讓瀏覽器不讀緩衝的圖片,從而實現重新整理驗證碼效果!

程式碼範例
  1. $str="QWERTYUIOPASDFGHJKLZXCVBNM1234567890";
  2. $image=imagecreate(50,25);
  3. imagecolorallocate($image,mt_rand(0,125),mt_rand(0,125),mt_rand(0,125));
  4. $color = imagecolorallocate($image,mt_rand(200,255),mt_rand(200,255),mt_rand(200,255));
  5. for($i=1;$i<=4;$i++) {
  6. $date=$str[mt_rand(0,strlen($str)-1)];
  7. $code.=$date;
  8. }
  9. session_start();
  10. $_SESSION['code'] = $code;
  11. imagestring($image,4,8,4,$code,$color);
  12. for($i=1;$i<=30;$i++) {
  13. imagesetpixel($image,mt_rand(0,50),mt_rand(0,25),mt_rand(125,200));
  14. }
  15. for($i=1;$i<=mt_rand(1,5);$i++) { imageline($image,mt_rand(0,50),mt_rand(0,25),mt_rand(0,50),mt_rand(0,25),mt_rand(100,150)); } header("content-type:image/png"); imagepng($image); ?>
  16. 數字+字母驗證碼(各字母顏色不同):
  17. $str="QWERTYUIOPASDFGHJKLZXCVBNM1234567890";
  18. $image=imagecreate(50,25);
  19. imagecolorallocate($image,mt_rand(0,125),mt_rand(0,125),mt_rand(0,125));
  20. $color[0] = imagecolorallocate($image,mt_rand(200,255),mt_rand(200,255),mt_rand(200,255));
  21. $color[1] = imagecolorallocate($image,mt_rand(200,255),mt_rand(200,255),mt_rand(200,255));
  22. $color[2] = imagecolorallocate($image,mt_rand(200,255),mt_rand(200,255),mt_rand(200,255));
  23. $color[3] = imagecolorallocate($image,mt_rand(200,255),mt_rand(200,255),mt_rand(200,255));
  24. for($i=0;$i<4;$i++) {
  25. $date=$str[mt_rand(0,strlen($str)-1)];
  26. $code.=$date;
  27. imagestring($image,5,6+$i*10,4,$code[$i],$color[$i]);
  28. }
  29. session_start();
  30. $_SESSION['code'] = $code;
  31. for($i=1;$i<=30;$i++) {
  32. imagesetpixel($image,mt_rand(0,50),mt_rand(0,25),mt_rand(125,200));
  33. }
  34. for($i=1;$i<=mt_rand(1,5);$i++) {
  35. imageline($image,mt_rand(0,50),mt_rand(0,25),mt_rand(0,50),mt_rand(0,25),mt_rand(100,150));
  36. }
  37. header("content-type:image/png");
  38. imagepng($image);
複製代碼
來自:php驗證碼實現原理
驗證碼, 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.