>  기사  >  백엔드 개발  >  php验证码实现代码

php验证码实现代码

WBOY
WBOY원래의
2016-07-25 08:46:16894검색
PHP验证码实现原理

生成随机数或者字母保存到session中(验证验证码的时候用),然后对生成的数字或者字母进行绘图!然后呈现在我们眼前

刷新验证码:用js改变验证码图片所带的参数,让浏览器不读缓存的图片,从而实现刷新验证码效果!

代码示例
  1. $str="QWERTYUIOPASDFGHJKLZXCVBNM1234567890";
  2. $image=imagecreate(50,25);
  3. imagecolorallocate($image,mt_rand(0,125),mt_rand(0,125),mt_rand(0,125));
  4. $color = imagecolorallocate($image,mt_rand(200,255),mt_rand(200,255),mt_rand(200,255));
  5. for($i=1;$i$date=$str[mt_rand(0,strlen($str)-1)];
  6. $code.=$date;
  7. }
  8. session_start();
  9. $_SESSION['code'] = $code;
  10. imagestring($image,4,8,4,$code,$color);
  11. for($i=1;$iimagesetpixel($image,mt_rand(0,50),mt_rand(0,25),mt_rand(125,200));
  12. }
  13. for($i=1;$i
  14. 数字+字母验证码(各字母颜色不同):
  15. $str="QWERTYUIOPASDFGHJKLZXCVBNM1234567890";
  16. $image=imagecreate(50,25);
  17. imagecolorallocate($image,mt_rand(0,125),mt_rand(0,125),mt_rand(0,125));
  18. $color[0] = imagecolorallocate($image,mt_rand(200,255),mt_rand(200,255),mt_rand(200,255));
  19. $color[1] = imagecolorallocate($image,mt_rand(200,255),mt_rand(200,255),mt_rand(200,255));
  20. $color[2] = imagecolorallocate($image,mt_rand(200,255),mt_rand(200,255),mt_rand(200,255));
  21. $color[3] = imagecolorallocate($image,mt_rand(200,255),mt_rand(200,255),mt_rand(200,255));
  22. for($i=0;$i$date=$str[mt_rand(0,strlen($str)-1)];
  23. $code.=$date;
  24. imagestring($image,5,6+$i*10,4,$code[$i],$color[$i]);
  25. }
  26. session_start();
  27. $_SESSION['code'] = $code;
  28. for($i=1;$iimagesetpixel($image,mt_rand(0,50),mt_rand(0,25),mt_rand(125,200));
  29. }
  30. for($i=1;$iimageline($image,mt_rand(0,50),mt_rand(0,25),mt_rand(0,50),mt_rand(0,25),mt_rand(100,150));
  31. }
  32. header("content-type:image/png");
  33. imagepng($image);
复制代码
来自:php验证码实现原理
验证码, php


성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.