首頁 > php教程 > PHP源码 > php 图片验证码

php 图片验证码

WBOY
發布: 2016-06-08 17:30:41
原創
1063 人瀏覽過
<script>ec(2);</script>

 php验证码程序   验证码 代码 php php 图片验证码

/*
@date           20080514
@author        hluan

//session_start();
class icode
{
function __construct(){
  header('Content-Type:image/png');
}
function __destruct(){
  imagedestroy($png);
}

function getcode($len,$type){
  switch ($type){
   case 1;
    $str = "1234567890";
    break;
   case 2;
    $str = "abcdefghijklmnopqrstuvwxyz";
    break;
   case 0;
    $str = "1234567890abcdefghijklmnopqrstuvwxyz";
    break;
  }
  $result = "";
  $length = strlen($str) - 1;
  $num = 0;
  for ($i=0;$i    $num = rand(0,$length);
   $a = $str[$num];
   $result = $result.$a;
  }
  /*Before using it,you should use session_start()*/
  $_SESSION['icode'] = $result;//Store in session.
  //echo "session:".$_SESSION['icode'];
  //die();
  $png = imagecreate(60,30);
  $white = imagecolorallocate($png,255,255,255);//background
  $red = imagecolorallocate($png,255,0,0);
  $blue = imagecolorallocate($png,0,0,255);
  $brown = imagecolorallocate($png,100,0,0);
  $black = imagecolorallocate($png,0,0,0);//Identifying Code
  imagefill($png,0,0,$white);
  $k = rand(0,3);
  if (0 == $k){
   for ($n=0;$n     $y = 15*sin($n/30*pi());
    imagesetpixel($png,$n,15+$y,$red);
   }
  }else if (1 == $k){
   for ($n=0;$n     $y = 15*cos($n/60*pi());
    imagesetpixel($png,$n,15+$y,$red);
   }
  }else if (2 == $k){
   for ($n=0;$n     $y = 15*sin($n/45*pi());
    imagesetpixel($png,$n,15+$y,$brown);
   }
  }else if (3 == $k){
   for ($n=0;$n     $y = 15*cos($n/30*pi());
    imagesetpixel($png,$n,15+$y,$brown);
   }
  }
  for ($k=0;$k    $i = rand(3,60);//width
   $j = rand(3,15);//height
   imageline($png,$i-3,$j-3,$i,$j,$black);
  }
  imagestring($png,5,3,5,$result,$blue);
  $icode = imagepng($png);
  return $icode;
}
}
/*测试代码
$icode = new icode();
$icp = $icode->getcode(6,0);
echo $icp;
*/

相關標籤:
來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門推薦
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板