下面是php生成验证码的源码:
<p><?php<br /></p>session_start();<br />//session_register('CheckCode'); <br />//PHP4.2以上版本不需要用session_register()注册SESSION变量<br />$type='gif';<br />$width= 45;<br />$height= 20;<br />header("Content-type: image/".$type);<br />srand((double)microtime()*1000000);<br />if(isset($_GET['action'])){<br /> $randval=randStr(4,$_GET['action']); <br />}else{<br /> $randval=randStr(4,''); <br />}<br />if($type!='gif'&&function_exists('imagecreatetruecolor')){<br /> $im=@imagecreatetruecolor($width,$height);<br />}else{<br /> $im=@imagecreate($width,$height);<br />}<br />$r=Array(225,211,255,223);<br />$g=Array(225,236,237,215);<br />$b=Array(225,236,166,125);<br />$key=rand(0,3);<br />$backColor=ImageColorAllocate($im,$r[$key],$g[$key],$b[$key]);//背景色(随机)<br />$borde<div>本文#来源gaodai.ma#com搞##代!^码7网</div><pre>搞代gaodaima码
rColor=ImageColorAllocate($im,127,157,185);//边框色
$pointColor=ImageColorAllocate($im,255,170,255);//点颜色
@imagefilledrectangle($im,0,0,$width – 1,$height – 1,$backColor);//背景位置
@imagerectangle($im,0,0,$width-1,$height-1,$borderColor); //边框位置
$stringColor=ImageColorAllocate($im,255,51,153);
for($i=0;$i<=100;$i++){
$pointX=rand(2,$width-2);
$pointY=rand(2,$height-2);
@imagesetpixel($im,$pointX,$pointY,$pointColor);
}
@imagestring($im,5,5,1,$randval,$stringColor);
$ImageFun=’Image’.$type;
$ImageFun($im);
@imagedestroy($im);
$_SESSION[‘CheckCode’]=$randval;
function randStr($len=6,$format=’ALL’){
switch($format){
case ‘ALL’://生成包含数字和字母的验证码
$chars=’ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789′; break;
case ‘CHAR’://仅生成包含字母的验证码
$chars=’ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz’; break;
case ‘NUMBER’://仅生成包含数字的验证码
$chars=’0123456789′; break;
default :
$chars=’ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789′; break;
}
$string=”;
while(strlen($string)<$len)
$string.=substr($chars,(mt_rand()%strlen($chars)),1);
return $string;
}
该函数的具体使用方法请看如下这个示例(这里是生成全数字的验证码):
相关文章
PHP中文验证码实现代码
Securimage PHP生成验证码类