<?php
session_start();
$img_width=75;
$img_height=22;
$nmsg='';
for($i=0;$i<4;$i++){
$nmsg.=dechex(mt_rand(0,15));
}
$_SESSION[login_check_number] = md5($nmsg);
$aimg = imageCreate($img_width,$img_height);
ImageColorAllocate($aimg, 255,255,255);
$black = ImageColorAllocate($aimg, 0,0,0);
ImageRectangle($aimg,0,0,$img_width-1,$img_height-1,$black);
for ($i=1; $i<=100; $i++) {
imageString($aimg,1,mt_rand(1,$img_width),mt_rand(1,$img_height),"*",imageColorAllocate($aimg,mt_rand(200,255),mt_rand(200,255),mt_rand(200,255)));
}
for ($i=0;$i<strlen($nmsg);$i++){
imageString($aimg, mt_rand(3,5),$i*$img_width/4+mt_rand(1,10),mt_rand(1,$img_height/2), $nmsg[$i],imageColorAllocate($aimg,mt_rand(0,100),mt_rand(0,150),mt_rand(0,200)));
}
Header("Content-type: image/png");
ImagePng($aimg);
ImageDestroy($aimg);
?>
调用方法:
<script language="javascript" type="text/javascript">
function change_pic(){ //更换验证码图片
document.getElementById("pic").innerHTML="<img src='pic.php?n=" + Math.round(Math.random()*10) + "' border='0' onclick='change_pic()' alt='看不清楚? 换一个'/>";
this.document.form1.c_code.focus();
}
</script>
<input name='c_code' type='text' size='7' maxlength="4" style="font-size:11px;" onfocus="change_pic();" />