Home > Backend Development > PHP Tutorial > Example of php generating verification code

Example of php generating verification code

WBOY
Release: 2016-07-25 09:04:24
Original
1018 people have browsed it
  1. index.html
  2. 验证码
  3. 看不清?换一个
复制代码

2、verifycode.php

  1. /*
  2. 图片验证码 Powered By KASON test http://bbs.it-home.org */
  3. session_start();
  4. $num=4;//验证码个数
  5. $width=80;//验证码宽度
  6. $height=20;//验证码高度
  7. $code=' ';
  8. for($i=0;$i<$num;$i++)//生成验证码
  9. {
  10. switch(rand(0,2))
  11. {
  12. case 0:$code[$i]=chr(rand(48,57));break;//数字
  13. case 1:$code[$i]=chr(rand(65,90));break;//大写字母
  14. case 2:$code[$i]=chr(rand(97,122));break;//小写字母
  15. }
  16. }
  17. $_SESSION["VerifyCode"]=$code;
  18. $image=imagecreate($width,$height);
  19. imagecolorallocate($image,255,255,255);
  20. for($i=0;$i<80;$i++)//生成干扰像素
  21. {
  22. $dis_color=imagecolorallocate($image,rand(0,2555),rand(0,255),rand(0,255));
  23. imagesetpixel($image,rand(1,$width),rand(1,$height),$dis_color);
  24. }
  25. for($i=0;$i<$num;$i++)//打印字符到图像
  26. {
  27. $char_color=imagecolorallocate($image,rand(0,2555),rand(0,255),rand(0,255));
  28. imagechar($image,60,($width/$num)*$i,rand(0,5),$code[$i],$char_color);
  29. }
  30. header("Content-type:image/png");
  31. imagepng($image);//输出图像到浏览器
  32. imagedestroy($image);//释放资源
  33. ?>
复制代码

3、checkcode.php

  1. ini_set('display_errors', 'Off');
  2. session_start();
  3. if((strtoupper($_POST["code"])) == strtoupper(($_SESSION["VerifyCode"]))){
  4. print("验证码正确,");
  5. }else{
  6. print("验证码错误,");
  7. }
  8. echo "提交的验证码:".strtoupper($_POST["code"]).",正确的验证码:".strtoupper($_SESSION["VerifyCode"]);
  9. ?>
复制代码


Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template