验证码

 <?php

header("content-type:image/gif");
include 'util.php';
$length = isset($_GET['n']) ? $_GET['n'] : 4;
$w = $length*20;
$h = 25;
$dotnum = 2;
$linenum = 2;
$i =imagecreatetruecolor($w,$h);
$bgc =imagecolorallocate($i,251,251,251);
imagefilledrectangle($i,0,0,$w,$h,$bgc);
//绘制干扰线
for($n=0;$n<$linenum;$n++){
$cc=imagecolorallocate($i,mt_rand(0,255),mt_rand(0,255),mt_rand(0,255));
imageline($i,mt_rand(0,$w),mt_rand(0,$h),mt_rand(0,$w),mt_rand(0,$h),$cc);
}
//绘制干扰点
for($n=0;$n<$dotnum;$n++){
$cc=imagecolorallocate($i,mt_rand(0,255),mt_rand(0,255),mt_rand(0,255));
$size=mt_rand(2,6);
imagefilledellipse($i,mt_rand(0,$w),mt_rand(0,$h),$size,$size,$cc);
}
$text =getstr($length,true,true);
for($n=0;$n<$length;$n++){
$c = imagecolorallocate($i,mt_rand(0,255),mt_rand(0,255),mt_rand(0,255));
imagettftext($i,mt_rand(12,18),mt_rand(-10,10),$n*20,mt_rand(18,22),$c,'b.ttf',substr($text,$n,1));
}
imagegif($i);
imagedestroy($i);
 
 
//util.php
<?php
/**
 * 功能:返回随机字符串 $len 多少个 $num 是否包含数字  $word 是否包含大写字母
 */
function getString($len=4,$num=true,$word=true){
   $str = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
   if(!$num){
  $str = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
   }
   if(!$word){
       $str = 'abcdefghijklmnopqrstuvwxyz';
  if($num){
      $str = '0123456789abcdefghijklmnopqrstuvwxyz';   
  }
   }
   $s = '';
   for($i=0;$i<$len;$i++){
  $s.=$str[mt_rand(0,strlen($str)-1)];
   }
   return $s;
}
function getstr($length=4,$num=true,$word=true){
$n=0123456789;
$w='abcdefghijklmnopqrstuvwxyz';
$ww='ABCDEFGHIJKLMNOPQRSTUVWXYZ';
$temp='';
if($num && $word){
$t=$n.$w.$ww;
}else if($num){
$t=$n.$w;
}else if($word){
$t=$w.$ww;
}else{
$t=$w;
}
for($i=0;$i<$length;$i++){
$temp.=substr($t,mt_rand(0,strlen($t)-1),1);
}
return $temp;
}
//echo getstr(4,true,false);

 

你可能感兴趣的:(验证码)