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

Example of generating verification code by php GD library

WBOY
Release: 2016-07-25 09:04:13
Original
825 people have browsed it
  1. 验证码
  2. 请输入验证码

复制代码

2、生成验证码 auth.php

  1. session_start();

  2. header("Content-type:image/png");

  3. $img_width=100;

  4. $img_height=20;

  5. srand(microtime()*100000);

  6. for($i=0;$i<4;$i++)
  7. {
  8. $new_number.=dechex(rand(0,15));
  9. }

  10. $_SESSION[check_auth]=$new_number;

  11. $new_number=imageCreate($img_width,$img_height);//创建图象
  12. ImageColorAllocate($new_number,255,255,255); //设置背景色为白色

  13. for($i=0;$i

  14. {
  15. $font=mt_rand(3,5);
  16. $x=mt_rand(1,8) + $img_width*$i/4;
  17. $y=mt_rand(1,$img_height/4);
  18. $color=imageColorAllocate($new_number,mt_rand(0,100),mt_rand(0,150),mt_rand(0,200));//设置字符颜色
  19. imageString($new_number,$font,$x,$y,$_SESSION[check_auth][$i],$color);//输出字符
  20. }

  21. ImagePng($new_number);

  22. ImageDestroy($new_number);
  23. ?>

复制代码

3、提交页面 check_auth.php

  1. session_start();
  2. $auth=$_POST['auth'];

  3. if(empty($auth))

  4. {
  5. echo '错误:验证码不能为空';
  6. die;
  7. }

  8. if($auth==$_SESSION['check_auth'])

  9. {
  10. echo '正确';
  11. }
  12. else
  13. {
  14. echo '错误:验证码输入错误';
  15. }
  16. ?>

复制代码


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