PHP 數字驗證碼的例子_PHP教程

來源:互聯網
上載者:User
在php中實現驗證碼還是很方便的,關鍵點在於掌握php gd庫與session的用法。

縱觀網上php 產生驗證碼的例子,無不是php gd庫與session相結合,並利用php 產生隨機數的方法來完成。

PHP驗證碼,可以分為很多種,包括 php 圖片驗證碼,php 隨機驗證碼,以及php 中文驗證碼等,根據不同的應用場合來使用不同的驗證碼。

這裡分享一個php數字驗證碼,供大家參考。


1,數字驗證碼
  1. /*
  2. *Filename:authpage.php
  3. */
  4. session_start();
  5. //srand((double)microtime()*1000000);
  6. $authnum=$_SESSION['authnum'];
  7. //驗證使用者輸入是否和驗證碼一致
  8. if(isset($_POST['authinput']))
  9. {
  10. if(strcmp($_POST['authinput'],$_SESSION['authnum'])==0)
  11. echo"驗證成功!";
  12. else
  13. echo"驗證失敗!";
  14. }
  15. //產生新的四位整數驗證碼
  16. //while(($authnum=rand()%10000)<1000);
  17. ?>
2,authimg.php
  1. //產生驗證碼圖片
  2. Header("Content-type:image/PNG");
  3. srand((double)microtime()*1000000);//播下一個產生隨機數位種子,以方便下面隨機數產生的使用
  4. session_start();//將隨機數存入session中
  5. $_SESSION['authnum']="";
  6. $im=imagecreate(62,20);//制定圖片背景大小
  7. $black=ImageColorAllocate($im,0,0,0);//設定三種顏色
  8. $white=ImageColorAllocate($im,255,255,255);
  9. $gray=ImageColorAllocate($im,200,200,200);
  10. imagefill($im,0,0,$gray);//採用地區填充法,設定(0,0)
  11. while(($authnum=rand()%100000)<10000);
  12. //將四位整數驗證碼繪入圖片
  13. // www.jbxue.com
  14. $_SESSION['authnum']=$authnum;
  15. imagestring($im,5,10,3,$authnum,$black);
  16. //用col顏色將字串s畫到image所代表的映像的x,y座標處(映像的左上方為0,0)。
  17. //如果font是1,2,3,4或5,則使用內建字型
  18. for($i=0;$i<200;$i++)//加入幹擾象素
  19. {
  20. $randcolor=ImageColorallocate($im,rand(0,255),rand(0,255),rand(0,255));
  21. imagesetpixel($im,rand()%70,rand()%30,$randcolor);
  22. }
  23. ImagePNG($im);
  24. ImageDestroy($im);
  25. ?>

http://www.bkjia.com/PHPjc/634161.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/634161.htmlTechArticle在php中實現驗證碼還是很方便的,關鍵點在於掌握php gd庫與session的用法。 縱觀網上php 產生驗證碼的例子,無不是php gd庫與session相結合,並...

  • 聯繫我們

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