Home > Backend Development > PHP Tutorial > PHP implements simple image verification code

PHP implements simple image verification code

WBOY
Release: 2016-07-25 08:46:15
Original
1121 people have browsed it

This is the simplest picture verification code:

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. ? >
Copy code

form.php
  1. php implements simple image verification code
  2. more0
  3. php
  4. Verification code
  5. This is the simplest image verification code:
  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
  25. Random Number

  26. < input type="submit" name="submit" value="Check">
Copy code

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. ?>
Copy code


Verification code, php


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