Heim  >  Artikel  >  Backend-Entwicklung  >  生成验证码 随机干扰php实现

生成验证码 随机干扰php实现

WBOY
WBOYOriginal
2016-07-25 08:46:03934Durchsuche
  1. session_start ();
  2. header ( 'Content-type: image/png' );
  3. //创建图片
  4. $im = imagecreate($x=130,$y=45 );
  5. $bg = imagecolorallocate($im,rand(50,200),rand(0,155),rand(0,155)); //第一次对 imagecolorallocate() 的调用会给基于调色板的图像填充背景色
  6. $fontColor = imageColorAllocate ( $im, 255, 255, 255 ); //字体颜色
  7. $fontstyle = 'rock.ttf'; //字体样式,这个可以从c:\windows\Fonts\文件夹下找到,我把它放到和authcode.php文件同一个目录,这里可以替换其他的字体样式
  8. //产生随机字符
  9. for($i = 0; $i $randAsciiNumArray = array (rand(48,57),rand(65,90));
  10. $randAsciiNum = $randAsciiNumArray [rand ( 0, 1 )];
  11. $randStr = chr ( $randAsciiNum );
  12. imagettftext($im,30,rand(0,20)-rand(0,25),5+$i*30,rand(30,35),$fontColor,$fontstyle,$randStr);
  13. $authcode .= $randStr;
  14. }
  15. $_SESSION['authcode'] = $randFourStr;//用户和用户输入的验证码做比较
  16. //干扰线
  17. for ($i=0;$i $lineColor = imagecolorallocate($im,rand(0,255),rand(0,255),rand(0,255));
  18. imageline ($im,rand(0,$x),0,rand(0,$x),$y,$lineColor);
  19. }
  20. //干扰点
  21. for ($i=0;$i imagesetpixel($im,rand(0,$x),rand(0,$y),$fontColor);
  22. }
  23. imagepng($im);
  24. imagedestroy($im);
  25. ?>
复制代码

验证码, php


Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn