国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發 > PHP > 正文

PHP實現簡單實用的驗證碼類

2024-05-04 23:38:15
字體:
來源:轉載
供稿:網友

這篇文章主要介紹了PHP實現簡單實用的驗證碼類,包含驗證碼常用的隨機驗證碼、干擾線、圖片生成與輸出等相關技巧,具有一定參考借鑒價值,需要的朋友可以參考下

本文實例講述了PHP實現簡單實用的驗證碼類。分享給大家供大家參考。具體如下:

 

 
  1. <?php 
  2. /** 
  3. * @version 1.0 
  4. * @author bolted snail 
  5. * @date 2011-10-15 
  6. * @PHP驗證碼類 
  7. * 使用方法: 
  8. * $image=new Captcha(); 
  9. * $image->config('寬度','高度','字符個數','驗證碼session索引'); 
  10. * $image->create();//這樣就會向瀏覽器輸出一張圖片 
  11. * //所有參數都可以省略, 
  12. * 默認是:寬80 高20 字符數4 驗證碼session索引captcha_code 
  13. * 第四個參數即把驗證碼存到$_SESSION['captcha_code'] 
  14. * 最簡單使用示例: 
  15. * $image=new Captcha(); 
  16. * $image->create();//這樣就會向瀏覽器輸出一張圖片 
  17. */ 
  18. class Captcha 
  19. private $width=80,$height=20,$codenum=4; 
  20. public $checkcode; //產生的驗證碼 
  21. private $checkimage; //驗證碼圖片  
  22. private $disturbColor = ''//干擾像素 
  23. private $session_flag='captcha_code';//存到session中的索引 
  24. //嘗試開始session 
  25. function __construct(){ 
  26. @session_start(); 
  27. /* 
  28. * 參數:(寬度,高度,字符個數) 
  29. */ 
  30. function config($width='80',$height='20',$codenum='4',$session_flag='captcha_code'
  31. {  
  32. $this->width=$width; 
  33. $this->height=$height; 
  34. $this->codenum=$codenum; 
  35. $this->session_flag=$session_flag; 
  36. function create() 
  37. //輸出頭 
  38. $this->outFileHeader(); 
  39. //產生驗證碼 
  40. $this->createCode(); 
  41. //產生圖片 
  42. $this->createImage(); 
  43. //設置干擾像素 
  44. $this->setDisturbColor(); 
  45. //往圖片上寫驗證碼 
  46. $this->writeCheckCodeToImage(); 
  47. imagepng($this->checkimage); 
  48. imagedestroy($this->checkimage); 
  49. $_SESSION[$this->session_flag]=$this->checkcode; 
  50. /* 
  51. * @brief 輸出頭 
  52. */ 
  53. private function outFileHeader() 
  54. header ("Content-type: image/png"); 
  55. /** 
  56. * 產生驗證碼 
  57. */ 
  58. private function createCode() 
  59. $this->checkcode = strtoupper(substr(md5(rand()),0,$this->codenum)); 
  60. /** 
  61. * 產生驗證碼圖片 
  62. */ 
  63. private function createImage() 
  64. $this->checkimage = @imagecreate($this->width,$this->height); 
  65. $back = imagecolorallocate($this->checkimage,255,255,255);  
  66. $border = imagecolorallocate($this->checkimage,0,0,0);  
  67. imagefilledrectangle($this->checkimage,0,0,$this->width - 1,$this->height - 1,$back); // 白色底 
  68. imagerectangle($this->checkimage,0,0,$this->width - 1,$this->height - 1,$border); // 黑色邊框 
  69. /** 
  70. * 設置圖片的干擾像素  
  71. */ 
  72. private function setDisturbColor() 
  73. for ($i=0;$i<=200;$i++) 
  74. $this->disturbColor = imagecolorallocate($this->checkimage, rand(0,255), rand(0,255), rand(0,255)); 
  75. imagesetpixel($this->checkimage,rand(2,128),rand(2,38),$this->disturbColor); 
  76. /** 
  77. * 
  78. * 在驗證碼圖片上逐個畫上驗證碼 
  79. * 
  80. */ 
  81. private function writeCheckCodeToImage() 
  82. for ($i=0;$i<$this->codenum;$i++) 
  83. $bg_color = imagecolorallocate ($this->checkimage, rand(0,255), rand(0,128), rand(0,255)); 
  84. $x = floor($this->width/$this->codenum)*$i; 
  85. $y = rand(0,$this->height-15); 
  86. imagechar ($this->checkimage, rand(5,8), $x+5, $y, $this->checkcode[$i], $bg_color); 
  87. function __destruct() 
  88. unset($this->width,$this->height,$this->codenum,$this->session_flag); 
  89. ?> 

希望本文所述對大家的php程序設計有所幫助。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 灵丘县| 镇坪县| 木兰县| 弋阳县| 平度市| 明光市| 丹棱县| 武强县| 夏邑县| 阳高县| 邻水| 巴彦淖尔市| 霍州市| 锦州市| 南平市| 苍山县| 安龙县| 周口市| 沾化县| 准格尔旗| 浮山县| 丰原市| 元江| 建昌县| 广平县| 东宁县| 锦州市| 绥德县| 三江| 彩票| 新竹县| 新野县| 扎兰屯市| 柯坪县| 台前县| 舒城县| 拜城县| 灵丘县| 新巴尔虎右旗| 汪清县| 开平市|