当前位置:Gxlcms > PHP教程 > php实现简单的图片验证码

php实现简单的图片验证码

时间:2021-07-01 10:21:17 帮助过:11人阅读

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

image.php

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

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<6;$i++){
  10. $pos = rand(0,36);
  11. $str .= $string{$pos};
  12. }
  13. $img_handle = ImageCreate (60, 20) or die ("Cannot Create image");
  14. //Image size (x,y)
  15. $back_color = ImageColorAllocate($img_handle, 255, 255, 255);
  16. //Background color RBG
  17. $txt_color = ImageColorAllocate($img_handle, 0, 0, 0);
  18. //Text Color RBG
  19. ImageString($img_handle, 31, 5, 0, $str, $txt_color);
  20. Imagepng($img_handle);
  21. session_start();
  22. $_SESSION['img_number'] = $str;
  23. ?>
  24. form.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

人气教程排行