Home >Backend Development >PHP Tutorial >Generate verification code to randomly interfere with PHP implementation

Generate verification code to randomly interfere with PHP implementation

WBOY
WBOYOriginal
2016-07-25 08:46:031000browse
  1. session_start ();
  2. header ( 'Content-type: image/png' );
  3. //Create image
  4. $im = imagecreate($x=130,$y=45 );
  5. $bg = imagecolorallocate($im,rand(50,200),rand(0,155),rand(0,155)); //The first call to imagecolorallocate() will fill the background color of the palette-based image
  6. $fontColor = imageColorAllocate ($im, 255, 255, 255); //Font color
  7. $fontstyle = 'rock.ttf'; //Font style, this can be found in the c:windowsFonts folder, I put it in and authcode. php file, you can replace other font styles here
  8. //Generate random characters
  9. for($i = 0; $i < 4; $i ++) {
  10. $randAsciiNumArray = array (rand(48,57 ),rand(65,90));
  11. $randAsciiNum = $randAsciiNumArray [rand ( 0, 1 )];
  12. $randStr = chr ( $randAsciiNum );
  13. imagettftext($im,30,rand(0,20)- rand(0,25),5+$i*30,rand(30,35),$fontColor,$fontstyle,$randStr);
  14. $authcode .= $randStr;
  15. }
  16. $_SESSION['authcode'] = $randFourStr;//Compare the user with the verification code entered by the user
  17. //Interference line
  18. for ($i=0;$i<8;$i++){
  19. $lineColor = imagecolorallocate($im,rand(0,255) ,rand(0,255),rand(0,255));
  20. imageline ($im,rand(0,$x),0,rand(0,$x),$y,$lineColor);
  21. }
  22. //Interference Click
  23. for ($i=0;$i<250;$i++){
  24. imagesetpixel($im,rand(0,$x),rand(0,$y),$fontColor);
  25. }
  26. imagepng($ im);
  27. imagedestroy($im);
  28. ?>
Copy code

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