Home >Backend Development >PHP Tutorial >A relatively stable PHP login system verification code

A relatively stable PHP login system verification code

WBOY
WBOYOriginal
2016-07-25 09:07:13907browse
  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<=100;$i++){
  24. $pointX = rand(2,$width-2);
  25. $pointY = rand(2,$height-2);
  26. @imagesetpixel($im, $pointX, $pointY, $pointColor);
  27. }
  28. @imagestring($im, 16, 10, 2, $randval, $stringColor);
  29. $ImageFun='Image'.$type;
  30. $ImageFun($im);
  31. @ImageDestroy($im);
  32. $_SESSION['validatecode'] = $randval;
  33. //产生随机字符串
  34. function randStr($len=6,$format='ALL') {
  35. switch($format) {
  36. case 'ALL':
  37. $chars='ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'; break;
  38. case 'CHAR':
  39. $chars='ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'; break;
  40. case 'NUMBER':
  41. $chars='0123456789'; break;
  42. default :
  43. $chars='ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
  44. break;
  45. }
  46. $string="";
  47. while(strlen($string)<$len)
  48. $string.=substr($chars,(mt_rand()%strlen($chars)),1);
  49. return $string;
  50. }
  51. ?>
复制代码



Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn