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

php实现简单的图片验证码

程序员文章站 2022-04-20 11:27:11
...

这是最简单的图片验证码:

image.php

  1. header("Content-type: image/png");
  2. $string = "abcdefghijklmnopqrstuvwxyz0123456789";
  3. for($i=0;$i $pos = rand(0,36);
  4. $str .= $string{$pos};
  5. }
  6. $img_handle = ImageCreate (60, 20) or die ("Cannot Create image");
  7. //Image size (x,y)
  8. $back_color = ImageColorAllocate($img_handle, 255, 255, 255);
  9. //Background color RBG
  10. $txt_color = ImageColorAllocate($img_handle, 0, 0, 0);
  11. //Text Color RBG
  12. ImageString($img_handle, 31, 5, 0, $str, $txt_color);
  13. Imagepng($img_handle);
  14. session_start();
  15. $_SESSION['img_number'] = $str;
  16. ?>
复制代码

form.php
  1. php实现简单的图片验证码
  2. 更多 0
  3. php
  4. 验证码
  5. 这是最简单的图片验证码:
  6. image.php
  7. header("Content-type: image/png");
  8. $string = "abcdefghijklmnopqrstuvwxyz0123456789";
  9. for($i=0;$i $pos = rand(0,36);
  10. $str .= $string{$pos};
  11. }
  12. $img_handle = ImageCreate (60, 20) or die ("Cannot Create image");
  13. //Image size (x,y)
  14. $back_color = ImageColorAllocate($img_handle, 255, 255, 255);
  15. //Background color RBG
  16. $txt_color = ImageColorAllocate($img_handle, 0, 0, 0);
  17. //Text Color RBG
  18. ImageString($img_handle, 31, 5, 0, $str, $txt_color);
  19. Imagepng($img_handle);
  20. session_start();
  21. $_SESSION['img_number'] = $str;
  22. ?>
  23. form.php
  24. php实现简单的图片验证码

复制代码

result.php

  1. session_start();
  2. if($_SESSION['img_number'] != $_POST['num']){
  3. echo'The number you entered doesn't match the image.
  4. Try Again
    ';
  5. }else{
  6. echo'The numbers Match!
  7. Try Again
    ';
  8. }
  9. ?>
复制代码


验证码, php