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

首頁 > 語言 > PHP > 正文

實用PHP驗證碼類代碼

2024-09-04 11:44:32
字體:
來源:轉載
供稿:網友

開發應用中驗證碼是少不了的,我們經常會碰以關于被機器注冊,那么有了驗證碼可以有效的防止這類行為,下面我們來看看我提供的這款代碼,實例代碼如下:

  1. <?php 
  2. session_start();  
  3. class authnum {  
  4. //圖片對象、寬度、高度、驗證碼長度  
  5. private $im;  
  6. private $im_width;  
  7. private $im_height;  
  8. private $len;  
  9. //隨機字符串、y軸坐標值、隨機顏色  
  10. private $randnum;  
  11. private $y;  
  12. private $randcolor;  
  13. //背景色的紅綠藍,默認是淺灰色  
  14. public $red=238;  
  15. public $green=238;  
  16. public $blue=238;  
  17. /**  
  18. * 可選設置:驗證碼類型、干擾點、干擾線、y軸隨機  
  19. * 設為 false 表示不啟用  
  20. **/  
  21. //默認是大小寫數字混合型,1 2 3 分別表示 小寫、大寫、數字型  
  22. public $ext_num_type='';  
  23. public $ext_pixel = false; //干擾點  
  24. public $ext_line = false; //干擾線  
  25. public $ext_rand_y= true; //y軸隨機  
  26. function __construct ($len=4,$im_width='',$im_height=25) {  
  27. // 驗證碼長度、圖片寬度、高度是實例化類時必需的數據  
  28. $this->len = $len$im_width = $len * 15;  
  29. $this->im_width = $im_width;  
  30. $this->im_height= $im_height;  
  31. $this->im = imagecreate($im_width,$im_height);  
  32. }  
  33. // 設置圖片背景顏色,默認是淺灰色背景  
  34. function set_bgcolor () {  
  35. imagecolorallocate($this->im,$this->red,$this->green,$this->blue);  
  36. }  
  37. // 獲得任意位數的隨機碼  
  38. function get_randnum () {  
  39. $an1 = 'abcdefghijklmnopqrstuvwxyz';  
  40. $an2 = 'abcdefghijklmnopqrstuvwxyz';  
  41. $an3 = '0123456789';  
  42. if ($this->ext_num_type == ''$str = $an1.$an2.$an3;  
  43. if ($this->ext_num_type == 1) $str = $an1;  
  44. if ($this->ext_num_type == 2) $str = $an2;  
  45. if ($this->ext_num_type == 3) $str = $an3;  
  46. for ($i = 0; $i < $this->len; $i++) {  
  47. $start = rand(1,strlen($str) - 1);  
  48. $randnum .= substr($str,$start,1);  
  49. }  
  50. $this->randnum = $randnum;  
  51. $_session[an] = $this->randnum;  
  52. }  
  53. // 獲得驗證碼圖片y軸  
  54. function get_y () {  
  55. if ($this->ext_rand_y) $this->y = rand(5, $this->im_height/5);  
  56. else $this->y = $this->im_height / 4 ;  
  57. }  
  58.  
  59. // 獲得隨機色  
  60. function get_randcolor () {  
  61. $this->randcolor = imagecolorallocate($this->im,rand(0,100),rand(0,150),rand(0,200));  
  62. }  
  63. // 添加干擾點  
  64. function set_ext_pixel () {  
  65. if ($this->ext_pixel) {  
  66. for($i = 0; $i < 100; $i++){  
  67. $this->get_randcolor();  
  68. imagesetpixel($this->im, rand()%100, rand()%100, $this->randcolor);  
  69. }  
  70. }  
  71. }  
  72. // 添加干擾線  
  73. function set_ext_line () {  
  74. if ($this->ext_line) {  
  75. for($j = 0; $j < 2; $j++){  
  76. $rand_x = rand(2, $this->im_width);  
  77. $rand_y = rand(2, $this->im_height);  
  78. $rand_x2 = rand(2, $this->im_width);  
  79. $rand_y2 = rand(2, $this->im_height);  
  80. $this->get_randcolor();  
  81. imageline($this->im, $rand_x$rand_y$rand_x2$rand_y2$this->randcolor);  
  82. }  
  83. }  
  84. }  
  85. /**創建驗證碼圖像:  
  86. * 建立畫布(__construct函數)  
  87. * 設置畫布背景($this->set_bgcolor();)  
  88. * 獲取隨機字符串($this->get_randnum ();)  
  89. * 文字寫到圖片上(imagestring函數)  
  90. * 添加干擾點/線($this->set_ext_line(); $this->set_ext_pixel();)  
  91. * 輸出圖片  
  92. **/  
  93. function create () {  
  94. $this->set_bgcolor();  
  95. $this->get_randnum ();  
  96. for($i = 0; $i < $this->len; $i++){  
  97. $font = rand(4,6);  
  98. $x = $i/$this->len * $this->im_width + rand(1, $this->len);  
  99. $this->get_y();  
  100. $this->get_randcolor();  
  101. imagestring($this->im, $font$x$this->y, substr($this->randnum, $i ,1), $this->randcolor);  
  102. }  
  103. $this->set_ext_line();  
  104. $this->set_ext_pixel();  
  105. header("content-type:image/png");  
  106. imagepng($this->im);  
  107. imagedestroy($this->im); //釋放圖像資源  
  108. }  
  109. }//end class  
  110. /**使用驗證碼類的方法:  
  111. * $an = new authnum(驗證碼長度,圖片寬度,圖片高度);  
  112. * 實例化時不帶參數則默認是四位的60*25尺寸的常規驗證碼圖片  
  113. * 表單頁面檢測驗證碼的方法,對比 $_session[an] 是否等于 $_post[驗證碼文本框id]  
  114. * 可選配置:  
  115. * 1.驗證碼類型:$an->ext_num_type=1; 值為1是小寫類型,2是大寫類型,3是數字類型  
  116. * 2.干擾點:$an->ext_pixel = false; 值為false表示不添加干擾點  
  117. * 3.干擾線:$an->ext_line = false; 值為false表示不添加干擾線  
  118. * 4.y軸隨機:$an->ext_rand_y = false; 值為false表示不支持圖片y軸隨機  
  119. * 5.圖片背景:改變 $red $green $blue 三個成員變量的值即可  
  120. **/  
  121. $an = new authnum();  
  122. //開源代碼Vevb.com 
  123. $an->ext_num_type='';  
  124. $an->ext_pixel = true; //干擾點  
  125. $an->ext_line = false; //干擾線  
  126. $an->ext_rand_y= true; //y軸隨機  
  127. $an->green = 238;  
  128. $an->create();  
  129. ?> 

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 龙山县| 格尔木市| 鹤山市| 繁昌县| 南华县| 南昌市| 阿城市| 凤阳县| 抚宁县| 汉中市| 定陶县| 昌平区| 连州市| 扎赉特旗| 江陵县| 珲春市| 体育| 玉龙| 独山县| 江津市| 旺苍县| 禹州市| 朝阳县| 霍山县| 肃北| 交城县| 伽师县| 炎陵县| 大余县| 突泉县| 合阳县| 德州市| 德阳市| 青河县| 灵石县| 庐江县| 运城市| 永清县| 石柱| 韶山市| 阿拉善右旗|