Discuz教程网

一个PHP验证码类代码分享(已封装成类)

[复制链接]
authicon dly 发表于 2011-9-12 14:15:25 | 显示全部楼层 |阅读模式
代码如下:

  1. <?php
  2. session_start();
  3. Header("Content-type: image/gif");
  4. class SecurityCode
  5. {
  6. private $codes = '';
  7. function __construct()
  8. {
  9. $code = '0-1-2-3-4-5-6-7-8-9-A-B-C-D-E-F-G-H-I-J-K-L-M-N-O-P-Q-R-S-T-U-V-W-X-Y-Z';
  10. $codeArray = explode('-',$code);
  11. shuffle($codeArray);
  12. $this->codes = implode('',array_slice($codeArray,0,4));
  13. }
  14. public function CreateImg()
  15. {
  16. $_SESSION['check_pic'] = $this->codes;
  17. $img = imagecreate(70,25);
  18. imagecolorallocate($img,222,222,222);
  19. $testcolor1 = imagecolorallocate($img,255,0,0);
  20. $testcolor2 = imagecolorallocate($img,51,51,51);
  21. $testcolor3 = imagecolorallocate($img,0,0,255);
  22. $testcolor4 = imagecolorallocate($img,255,0,255);
  23. for ($i = 0; $i < 4; $i++)
  24. {
  25. imagestring($img,rand(5,6),8 + $i * 15,rand(2,8),$this->codes[$i],rand(1,4));
  26. }
  27. imagegif($img);
  28. }
  29. }
  30. $code = new SecurityCode();
  31. $code->CreateImg();
  32. $code = NULL;
  33. ?>
复制代码

封装成类之后,加入了构造函数,使用起来也方便些。你也可以继续完善下这个验证码类,比如加入析构函数,如何更节省内存等等。



上一篇:简单的PHP多图上传小程序代码
下一篇:PHP 验证码的实现代码
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

1314学习网 ( 浙ICP备10214163号 )

GMT+8, 2025-5-2 11:00

Powered by Discuz! X3.4

© 2001-2013 Comsenz Inc.

快速回复 返回顶部 返回列表