欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  后端开发

一个比较稳定的php登陆系统验证码

程序员文章站 2024-02-08 23:55:16
...
  1. session_start();
  2. $type = 'gif';
  3. $width= 56;
  4. $height= 22;
  5. header("Content-type: image/".$type);
  6. srand((double)microtime()*1000000);
  7. $randval = randStr(4,"NUMBER");
  8. if($type!='gif' && function_exists('imagecreatetruecolor')){
  9. $im = @imagecreatetruecolor($width,$height);
  10. }else{
  11. $im = @imagecreate($width,$height);
  12. }
  13. $r = Array(225,211,255,223);
  14. $g = Array(225,236,237,215);
  15. $b = Array(225,236,166,125);
  16. $key = rand(0,3);
  17. $backColor = ImageColorAllocate($im,$r[$key],$g[$key],$b[$key]);//背景色(随机)
  18. $borderColor = ImageColorAllocate($im, 0, 0, 0);//边框色
  19. $pointColor = ImageColorAllocate($im, 255, 170, 255);//点颜色
  20. @imagefilledrectangle($im, 0, 0, $width - 1, $height - 1, $backColor);//背景位置
  21. @imagerectangle($im, 0, 0, $width-1, $height-1, $borderColor); //边框位置
  22. $stringColor = ImageColorAllocate($im, 255,51,153);
  23. for($i=0;$i$pointX = rand(2,$width-2);
  24. $pointY = rand(2,$height-2);
  25. @imagesetpixel($im, $pointX, $pointY, $pointColor);
  26. }
  27. @imagestring($im, 16, 10, 2, $randval, $stringColor);
  28. $ImageFun='Image'.$type;
  29. $ImageFun($im);
  30. @ImageDestroy($im);
  31. $_SESSION['validatecode'] = $randval;
  32. //产生随机字符串
  33. function randStr($len=6,$format='ALL') {
  34. switch($format) {
  35. case 'ALL':
  36. $chars='ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'; break;
  37. case 'CHAR':
  38. $chars='ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'; break;
  39. case 'NUMBER':
  40. $chars='0123456789'; break;
  41. default :
  42. $chars='ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
  43. break;
  44. }
  45. $string="";
  46. while(strlen($string)$string.=substr($chars,(mt_rand()%strlen($chars)),1);
  47. return $string;
  48. }
  49. ?>
复制代码