当前位置:首页 > PHP教程 > php应用 > 列表

php数字运算验证码的实现代码

发布:smiling 来源: PHP粉丝网  添加日期:2021-06-14 23:27:54 浏览: 评论:0 

这篇文章主要介绍了php实现数字运算验证码的方法,具有一定借鉴价值,需要的朋友可以参考下,本文实例讲述了php实现数字运算验证码的方法,具体实现方法如下:

  1. <?php 
  2. getValidate(100,30); 
  3.    
  4. function getValidate($w,$h){ 
  5.  $img = imagecreate($w,$h); 
  6.    
  7.  $gray = imagecolorallocate($img,255,255,255); 
  8.  $black = imagecolorallocate($img,rand(0,200),rand(0,200),rand(0,200)); 
  9.  $red = imagecolorallocate($img, 255, 0, 0); 
  10.  $white = imagecolorallocate($img, 255, 255, 255); 
  11.  $green = imagecolorallocate($img, 0, 255, 0); 
  12.  $blue = imagecolorallocate($img, 0, 0, 255); 
  13.  imagefilledrectangle($img, 0, 0, 100, 30, $black); 
  14.    
  15.    
  16.  for($i = 0;$i < 80;$i++){ 
  17.   imagesetpixel($img, rand(0,$w), rand(0,$h), $gray); 
  18.  } 
  19.    
  20.    
  21.  $num1 = rand(1,99); 
  22.  $num2 = rand(1,99); 
  23.    
  24.  imagestring($img, 5, 5, rand(1,10), $num1, $red); 
  25.  imagestring($img,5,30,rand(1,10),getRand(), $white); 
  26.  imagestring($img,5,45,rand(1,10),$num2, $green); 
  27.  imagestring($img,5,65,rand(1,10),"=", $blue); 
  28.  imagestring($img,5,80,rand(1,10),"?", $red); 
  29.     
  30.    
  31.  header("content-type:image/png"); 
  32.  imagepng($img); 
  33.  imagedestroy($img); 
  34. } 
  35. function getRand(){ 
  36.  $code = rand(0,1); 
  37.  switch ($code) { 
  38.   case 0: 
  39.    return "+"; 
  40.    break; 
  41.   case 1: 
  42.    return "-"; 
  43.    break; 
  44.   default: 
  45.    # code... 
  46.    break; 
  47.  } 
  48. } 
  49.    
  50. ?>

Tags: php数字运算验证码

分享到: