3、驗證碼

  封裝的驗證碼類?

  1. <?php
  2. /*
  3. * 生成驗證碼
  4. */
  5. class Captcha
  6. {
  7. private $_width = 100;
  8. private $_height = 25;
  9. private $_number = 4; //顯示的驗證碼的字符個數
  10. private $_font = 15; //驗證碼字體大小
  11. private $_fontfile = 'STXINWEI.TTF';
  12. //創建驗證碼圖像
  13. public function makeImage()
  14. {
  15. # 1. 創建圖像資源(畫布)
  16. $image = imagecreatetruecolor($this->_width,$this->_height);
  17. //隨機填充顏色
  18. //mt_rand(0,255) 生成一個更具有唯一性的隨機數 #000 255
  19. $color = imagecolorallocate($image,mt_rand(100,255),mt_rand(100,255),mt_rand(100,255));
  20. imagefill($image,0,0,$color);
  21. # 2.繪制文字
  22. $code = $this -> makeCode(); //隨機生成驗證碼文字 ab3g
  23. $color = imagecolorallocate($image,mt_rand(0,100),mt_rand(0,100),mt_rand(0,100));
  24. for($i=0;$i<$this->_number;$i++){
  25. imagettftext($image,$this->_font,mt_rand(-30,30),$i*($this->_width/$this->_number)+5,20,$color,$this->_fontfile,$code[$i]);
  26. }
  27. # 3.繪制15條干擾線條
  28. for($i=0;$i<10;$i++){
  29. $color = imagecolorallocate($image,mt_rand(100,150),mt_rand(100,150),mt_rand(100,150));
  30. imageline($image,mt_rand(0,$this->_width),mt_rand(0,$this->_height),mt_rand(0,$this->_width),mt_rand(0,$this->_height),$color);
  31. }
  32. # 4.設置100個干擾像素點
  33. for($i=0;$i<100;$i++){
  34. imagesetpixel($image,mt_rand(0,$this->_width),mt_rand(0,$this->_height),$color);
  35. }
  36. # 5.將驗證碼保存起來嗎,便于后面再其他地方使用
  37. //只能使用session來存儲,session明天就會講到
  38. session_start();
  39. $_SESSION['captcha'] = $code;
  40. //在瀏覽器輸出、顯示一下
  41. header("Content-Type:image/png");
  42. imagepng($image);
  43. imagedestroy($image);
  44. }
  45. /**
  46. * 隨機產生隨機數
  47. */
  48. public function makeCode()
  49. {
  50. # 獲得字母的范圍(大寫字母、小寫字母)
  51. $lower = range('a','z'); //創建從小a到小z字符范圍的數組
  52. $upper = range('A','Z'); //創建從大A到大Z范圍的數組
  53. $number = range(3,9); //創建從3到9之間的數字
  54. //將上面的三個數組合并成一個數組
  55. $code = array_merge($lower,$upper,$number);
  56. # 打亂數組元素的順序
  57. shuffle($code);
  58. //隨機從上面的數組中篩選出n個字符,需要通過下標來取數組的元素
  59. $str = '';
  60. for($i=0;$i<$this->_number;$i++){
  61. $str .= $code[$i];
  62. }
  63. return $str;
  64. }
  65. /**
  66. * 驗證用戶輸入的驗證碼和我們生產的驗證碼是否一致
  67. * @param [str] $input [輸入驗證碼值]
  68. * @return
  69. */
  70. public function checkCode($input)
  71. {
  72. session_start();
  73. if(strtolower($code) == strtolower($_SESSION['captcha'])){
  74. //說明驗證碼正確
  75. //echo '驗證碼正確';
  76. return true;
  77. }else{
  78. //echo '驗證碼錯誤';
  79. return false;
  80. }
  81. }
  82. }
  83. ?>

  實例 - 驗證碼驗證(結合上面的驗證類)

  html頁面?

  1. <form action="captcha.php?act=verify" method="post">
  2. 驗證碼:<input type="text" name="captcha">
  3. <img src="captcha.php?act=show">
  4. <br>
  5. <input type="submit" value="提交">
  6. </form>

  驗證碼檢測 captcha.php 頁面?

  1. //接收地址欄上面的參數
  2. if($_GET['act']=='verify'){
  3. //說明是提交的表單
  4. //接收表單中用戶輸入的內容
  5. $code = $_POST['captcha'];
  6. //和創建的驗證碼進行比較
  7. session_start();
  8. //將用戶輸入的驗證碼 和 我們創建的統一小寫之后再進行比較
  9. if(strtolower($code) == strtolower($_SESSION['captcha'])){
  10. //說明驗證碼正確
  11. echo '驗證碼正確';
  12. }else{
  13. echo '驗證碼錯誤';
  14. }
  15. }else if($_GET['act']=='show'){
  16. //說明需要顯示一個圖片
  17. require 'Captcha.class.php';
  18. $captcha = new Captcha();
  19. $captcha -> makeImage();
  20. }