imgcodeChajian.php 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. <?php
  2. /**
  3. * 图片验证码
  4. */
  5. class imgcodeChajian extends Chajian{
  6. public function check($key,$val)
  7. {
  8. if(isempt($val))return false;
  9. $code = 'abc'.$val.'';
  10. $geval = c('cache')->get('code'.$key.'');
  11. if(md5($code)!=$geval)return false;
  12. c('cache')->del('code'.$key.'');
  13. return true;
  14. }
  15. public function show($key)
  16. {
  17. header("Content-type:image/gif");
  18. $a = rand(0,9);
  19. $b = rand(0,9);
  20. $h = 30;
  21. $code = 'abc'.($a+$b).'';
  22. $w = 70;
  23. $im = imagecreatetruecolor($w,$h);
  24. c('cache')->set('code'.$key.'', md5($code), 5*60);
  25. $bg = imagecolorallocate($im,255,255,255);
  26. imagefill($im,0,0,$bg); //添加背景颜色
  27. $black = imagecolorallocate($im,0,0,0);
  28. for($i=0;$i<2;$i++){//画线条
  29. $at1=imagecolorallocate($im,rand(0,255),rand(0,255),rand(0,255));
  30. imageline($im,0,rand(0,$h),$w,rand(0,$h),$at1);
  31. }
  32. for($i=0;$i<200;$i++){//画点
  33. $at1=imagecolorallocate($im,rand(0,255),rand(0,255),rand(0,255));
  34. imagesetpixel($im,rand(0,$w),rand(0,$h),$at1);
  35. }
  36. imagestring($im,5,rand(0,30),rand(0,$h-15),''.$a.'+'.$b.'=?',$black);
  37. imagegif($im);
  38. imagedestroy($im);
  39. }
  40. }
粤ICP备19079148号