免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 42641 | 回复: 2
打印 上一主题 下一主题

php简单验证码 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2011-12-20 23:06 |只看该作者 |倒序浏览

php简单验证码








Php代码
  1. 1.<!--  
  2. 2.*@copyright(c)2011  
  3. 3.*@author:Ernest  
  4. 4.*@time:2011  
  5. 5.*@version:new  
  6. 6.-->  
  7. 7.  
  8. 8.<?php  
  9. 9.  
  10. 10.class VerifyImg {  
  11. 11.    public $fontSize = 15; //定义字体大小  
  12. 12.    public $length = 4; //定义字符串长度                          
  13. 13.    public $width = 70; //定义图片宽度  
  14. 14.    public $height = 30; //定义图片高度  
  15. 15.    public $im = null; //生成一张指定宽高的图片  
  16. 16.    public $font = 'C:/Windows/Fonts/Arial.TTF';  
  17. 17.    public $strNum = "";  
  18. 18.    public function Build() {  
  19. 19.        $strings = Array ('1', '2','3', '4', '5', '6', '7', 'a', 'b', 'c', 'd', 'e', 'f', 'h', 'i', 'j', 'k', 'm', 'n', 'p', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y' );  
  20. 20.//      $strNum = "";  
  21. 21.        $count = count ( $strings );  
  22. 22.        for($i = 1; $i <= $this->length; $i ++) { //循环随机取字符生成字符串  
  23. 23.            $strNum .= $strings [rand ( 0, $count - 1 )];  
  24. 24.        }  
  25. 25.        session_start ();  
  26. 26.        $_SESSION ["verifycode"] = $strNum;  
  27. 27.        $this->im = imagecreate ( $this->width, $this->height );  
  28. 28.        $backgroundcolor = imagecolorallocate ( $this->im, 255, 255, 255 ); //生成背景色  
  29. 29.        $frameColor = imageColorAllocate ( $this->im, 0, 255, 0 );  
  30. 30.        for($i = 0; $i < $this->length; $i ++) {  
  31. 31.            $charY = ($this->height + 9) / 2 + rand ( - 1, 1 ); //定义字符Y坐标  
  32. 32.            $charX = $i * 15 + 8; //定义字符X坐标  
  33. 33.            //            $text_color = imagecolorallocate($this->im, 255, 0, 0);                                                //生成字符颜色  
  34. 34.            $text_color = imagecolorallocate ( $this->im, mt_rand ( 50, 255 ), mt_rand ( 50, 128 ), mt_rand ( 50, 200 ) );  
  35. 35.            $angle = rand ( - 20, 20 ); //生成字符角度  
  36. 36.            //写入字符  
  37. 37.            imagettftext ( $this->im, $this->fontSize, $angle, $charX, $charY, $text_color, $this->font, $strNum [$i] );  
  38. 38.        }  
  39. 39.        for($i = 0; $i <= 5; $i ++) { //循环画背景线  
  40. 40.            $linecolor = imagecolorallocate ( $this->im, mt_rand ( 0, 255 ), mt_rand ( 0, 255 ), mt_rand ( 0, 255 ) );  
  41. 41.            $linex = mt_rand ( 1, $this->width - 1 );  
  42. 42.            $liney = mt_rand ( 1, $this->height - 1 );  
  43. 43.            imageline ( $this->im, $linex, $liney, $linex + mt_rand ( 0, 4 ) - 2, $liney + mt_rand ( 0, 4 ) - 2, $linecolor );  
  44. 44.        }  
  45. 45.        for($i = 0; $i <= 32; $i ++) { //循环画背景点,生成麻点效果  
  46. 46.            $pointcolor = imagecolorallocate ( $this->im, mt_rand ( 0, 255 ), mt_rand ( 0, 255 ), mt_rand ( 0, 255 ) );  
  47. 47.            imagesetpixel ( $this->im, mt_rand ( 1, $this->width - 1 ), mt_rand ( 1, $this->height - 1 ), $pointcolor );  
  48. 48.        }  
  49. 49.        imagerectangle ( $this->im, 0, 0, $this->width - 1, $this->height - 1, $frameColor ); //画边框  
  50. 50.        ob_clean ();  
  51. 51.        header ( 'Content-type:image/png' );  
  52. 52.        imagepng ( $this->im );  
  53. 53.        imagedestroy ( $this->im );  
  54. 54.    }  
  55. 55.}  
  56. 56.$img = new VerifyImg ();  
  57. 57.$img->Build ();  
  58. 58.?>  
复制代码

论坛徽章:
0
2 [报告]
发表于 2011-12-21 21:52 |只看该作者
学习鸟 谢谢分享

论坛徽章:
0
3 [报告]
发表于 2019-05-13 15:57 |只看该作者
其实现在有很多第三方提供验证码验证的,有多种语言调用的,可以去了解下第三方SMSSDK这个,本人觉得还不错哦!
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP