code.init.php 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. <?php
  2. if (!defined('IS_INITPHP')) exit('Access Denied!');
  3. /*********************************************************************************
  4. * InitPHP 3.8.2 国产PHP开发框架 扩展类库-验证码
  5. *-------------------------------------------------------------------------------
  6. * 版权所有: CopyRight By initphp.com
  7. * 您可以自由使用该源码,但是在使用过程中,请保留作者信息。尊重他人劳动成果就是尊重自己
  8. *-------------------------------------------------------------------------------
  9. * Author:zhuli Dtime:2014-11-25
  10. ***********************************************************************************/
  11. class codeInit {
  12. private $width = 62;
  13. private $height = 20;
  14. /**
  15. * 获取随机数值
  16. * @return string 返回转换后的字符串
  17. */
  18. private function get_random_val($key) {
  19. srand((double)microtime()*1000000);
  20. while(($authnum=rand()%100000)<10000);
  21. session_start();
  22. $_SESSION[$key . 'initphp_code'] = $authnum;
  23. return $authnum;
  24. }
  25. /**
  26. * 获取验证码图片
  27. * @return string
  28. */
  29. public function getcode($key = 'user_') {
  30. Header("Content-type: image/PNG");
  31. $im = imagecreate($this->width, $this->height); //制定图片背景大小
  32. $black = imagecolorallocate($im, 0,0,0); //设定三种颜色
  33. $white = imagecolorallocate($im, 255,255,255);
  34. $gray = imagecolorallocate($im, 200,200,200);
  35. imagefill($im,0,0,$gray); //采用区域填充法,设定(0,0)
  36. $authnum = $this->get_random_val($key);
  37. imagestring($im, 5, 10, 3, $authnum, $black);
  38. // 用 col 颜色将字符串 s 画到 image 所代表的图像的 x,y 座标处(图像的左上角为 0, 0)。
  39. //如果 font 是 1,2,3,4 或 5,则使用内置字体
  40. for($i=0; $i<200; $i++) {
  41. $randcolor = imagecolorallocate($im,rand(0,255),rand(0,255),rand(0,255));
  42. imagesetpixel($im, rand()%70 , rand()%30 , $randcolor);
  43. }
  44. $a = imagepng($im);
  45. imagedestroy($im);
  46. return $a;
  47. }
  48. public function checkCode($code, $key = 'user_') {
  49. session_start();
  50. if ($_SESSION[$key . 'initphp_code'] == $code) return true;
  51. return false;
  52. }
  53. }