//
<!---显示验证码的html页面--> <html> <head> <title>yzm</title> <script language="javascript"> function yzm() { var num1=Math.round(Math.random()*10000000); var num=num1.toString().substr(0,4); document.write("<img name=codeimg src='yzm.php?num="+num+"'>"); } function code() { var num1=Math.round(Math.random()*10000000); var num=num1.toString().substr(0,4); document.codeimg.src="yzm.php?num="+num; } </script> </head> <body> <script>yzm()</script> <a href="javascript:code()">看不清</a> </body> </html>
//生成验证码图像的php文件<?php srand((double)microtime()*1000000); $im=imagecreate(42,16); $black=imagecolorallocate($im,0,0,0); $white=imagecolorallocate($im,255,255,255); $gray=imagecolorallocate($im,200,200,200); imagefill($im,0,0,$gray); for($i=0;$i<4;$i++) { $str=mt_rand(1,3); $size=mt_rand(3,6); $authnum=substr($_GET[num],$i,1); imagestring($im,$size,(2+$i*10),$str,$authnum,imagecolorallocate($im,rand(0,130),rand(0,130),rand(0,130))); } for($i=0;$i<200;$i++) { $randcolor=imagecolorallocate($im,rand(0,255),rand(0,255),rand(0,255)); imagesetpixel($im,rand()%70,rand()%30,$randcolor); } imagepng($im); imagedestroy($im); ?>
yzm.php与yzm.html放在配置了php环境的同一个目录之下.