Home  >  Article  >  Backend Development  >  PHP verification code implementation code

PHP verification code implementation code

WBOY
WBOYOriginal
2016-07-25 08:46:16933browse
PHP verification code implementation principle

Generate random numbers or letters and save them in the session (used when verifying the verification code), and then draw the generated numbers or letters! Then present them in front of our eyes

Refresh the verification code: Use js to change the parameters of the verification code image so that the browser does not read the cached image, thereby achieving the effect of refreshing the verification code!

Code example
  1. $str="QWERTYUIOPASDFGHJKLZXCVBNM1234567890";
  2. $image=imagecreate(50,25);
  3. imagecolorallocate($image,mt_rand(0,125),mt_rand(0,125),mt_rand( 0,125));
  4. $ color = imagecolorallocate($image,mt_rand(200,255),mt_rand(200,255),mt_rand(200,255));
  5. for($i=1;$i<=4;$i++) {
  6. $date=$str[mt_rand( 0,strlen($str)-1)];
  7. $code.=$date;
  8. }
  9. session_start();
  10. $_SESSION['code'] = $code;
  11. imagestring($image,4,8,4 ,$code,$color);
  12. for($i=1;$i<=30;$i++) {
  13. imagesetpixel($image,mt_rand(0,50),mt_rand(0,25),mt_rand(125,200) );
  14. }
  15. for($i=1;$i<=mt_rand(1,5);$i++) { imageline($image,mt_rand(0,50),mt_rand(0,25),mt_rand(0, 50),mt_rand(0,25),mt_rand(100,150)); } header("content-type:image/png"); imagepng($image); ?>
  16. Number + letter verification code (each letter has a different color ):
  17. $str="QWERTYUIOPASDFGHJKLZXCVBNM1234567890";
  18. $image=imagecreate(50,25);
  19. imagecolorallocate($image,mt_rand(0,125),mt_rand( 0,125),mt_rand(0,125));
  20. $color[0] = imagecolorallocate($image,mt_rand(200,255),mt_rand(200,255),mt_rand(200,255));
  21. $color[1] = imagecolorallocate($image,mt_rand (200,255),mt_rand(200,255),mt_rand(200,255));
  22. $color[2] = imagecolorallocate($image,mt_rand(200,255),mt_rand(200,255),mt_rand(200,255));
  23. $color[3] = imagecolorallocate($image,mt_rand(200,255),mt_rand(200,255),mt_rand(200,255));
  24. for($i=0;$i<4;$i++) {
  25. $date=$str[mt_rand(0,strlen ($str)-1)];
  26. $code.=$date;
  27. imagestring($image,5,6+$i*10,4,$code[$i],$color[$i]);
  28. }
  29. session_start();
  30. $_SESSION['code'] = $code;
  31. for($i=1;$i<=30;$i++) {
  32. imagesetpixel($image,mt_rand(0,50),mt_rand (0,25),mt_rand(125,200));
  33. }
  34. for($i=1;$i<=mt_rand(1,5);$i++) {
  35. imageline($image,mt_rand(0,50), mt_rand(0,25),mt_rand(0,50),mt_rand(0,25),mt_rand(100,150));
  36. }
  37. header("content-type:image/png");
  38. imagepng($image);
Copy code
From: PHP verification code implementation principle
Verification code, php


Statement:
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