code.init.php 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. <?php
  2. if (!defined('IS_INITPHP')) exit('Access Denied!');
  3. class codeInit {
  4. private $width = 62;
  5. private $height = 20;
  6. /**
  7. * 获取随机数值
  8. * @return string 返回转换后的字符串
  9. */
  10. private function get_random_val($key) {
  11. srand((double)microtime()*1000000);
  12. while(($authnum=rand()%100000)<10000);
  13. session_start();
  14. $_SESSION[$key . 'vaildcode_key'] = $authnum;
  15. return $authnum;
  16. }
  17. /**
  18. * 获取验证码图片
  19. * @return string
  20. */
  21. public function getcode($key = 'user_') {
  22. Header("Content-type: image/PNG");
  23. $im = imagecreate($this->width, $this->height); //制定图片背景大小
  24. $black = imagecolorallocate($im, 0,0,0); //设定三种颜色
  25. $white = imagecolorallocate($im, 255,255,255);
  26. $gray = imagecolorallocate($im, 200,200,200);
  27. imagefill($im,0,0,$gray); //采用区域填充法,设定(0,0)
  28. $authnum = $this->get_random_val($key);
  29. imagestring($im, 5, 10, 3, $authnum, $black);
  30. // 用 col 颜色将字符串 s 画到 image 所代表的图像的 x,y 座标处(图像的左上角为 0, 0)。
  31. //如果 font 是 1,2,3,4 或 5,则使用内置字体
  32. for($i=0; $i<200; $i++) {
  33. $randcolor = imagecolorallocate($im,rand(0,255),rand(0,255),rand(0,255));
  34. imagesetpixel($im, rand()%70 , rand()%30 , $randcolor);
  35. }
  36. $a = imagepng($im);
  37. imagedestroy($im);
  38. return $a;
  39. }
  40. public function checkCode($code, $key = 'user_') {
  41. session_start();
  42. if ($_SESSION[$key . 'initphp_code'] == $code) return true;
  43. return false;
  44. }
  45. }