HTML code:
Copy Code code as follows:
<! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">
<meta http-equiv= "Content-type" content= "text/html; charset=gb2312 "/>
<title> Simple Verification Code </title>
<script language= "javascript" src= "Js/checked.js" ></script>
<body>
<form id= "register" name= "register" method= "POST" >
<table align= "center" >
<tr>
<TD ><div align= "Right" > Verification Code:</div></td>
<TD ><input id= "Yzm" type= "text" name= "Yzm" size= "8" onblur= "Javascript:chkyzm" (form) "Onmouseover=" This.style.backgroundcolor= ' #ffffff ' "onmouseout=" this.style.backgroundcolor= ' #e8f4ff ' "/></td><td >
<input id= "yzm2" type= "hidden" value= ""/></td>
<TD align= "center" valign= "Middle" ><script>yzm () </script></td>
<TD ><a href= "Javascript:code ()" style= "Text-decoration:none" > Can't see clearly </a></td>
<TD width= "align=" "Center" ><div id= "yzm1" ><font color= "#999999" > Please enter the verification code </FONT></DIV ></td>
</tr>
</table>
</form>
</body>
JS Code:
Copy Code code as follows:
function Chkyzm (form) {//Validate code verification
if (form.yzm.value== "") {
Yzm1.innerhtml= "<font color= #FF0000 >x Verification code can not be empty </font>";
}else if (form.yzm.value!=form.yzm2.value) {
Yzm1.innerhtml= "<font color= #FF0000 >x Authentication Code input error </font>";
}else{
Yzm1.innerhtml= "<font color=green>√ Verification code input correct </font>";
}
}
function Yzm () {//Generate verification code
var num1=math.round (Math.random () *1000000);//Random decimal amplification
var num=num1.tostring (). substr (0,4);//Fetch 4-bit integer
var Yzm2=document.getelementbyid ("yzm2");
document.write ("Yzm2.value=num;
}
function code () {//Reset verification Code
var num1=math.round (Math.random () *1000000);
var num=num1.tostring (). substr (0,4);
var Yzm2=document.getelementbyid ("yzm2");
Document.codeimg.src= "yzm.php?num=" +num;
Yzm2.value=num;
}
yzm.php Code:
Copy Code code as follows:
<?php
Header ("Content-type:image/png");
$im =imagecreate (66,22); Create Canvas
$black =imagecolorallocate ($im, 0,0,0); Define Background
$white =imagecolorallocate ($im, 255,255,255); Define Background
$gray =imagecolorallocate ($im, 200,200,200); Define Background
Imagefill ($im, 0,0, $gray); Fill Color
For ($i =0 $i <4; $i + +) {//define 4-bit random number
$str =mt_rand (1,5); Defines the y-coordinate of the location of the random character
$size =mt_rand (6,9); Fonts that define random characters
$authnum =substr ($_get[num], $i, 1); Get the authentication code passed in the hyperlink
Imagestring ($im, $size, (3+ $i *15), $str, $authnum, Imagecolorallocate ($im, Rand (0,250), Rand (0,250), Rand (0,250)); Degree of ambiguity of rand (0,500) numbers
}//Horizontal output string
For ($i =0 $i <200; $i + +) {//execute for loop, add a blur background to the CAPTCHA
$randcolor =imagecolorallocate ($im, Rand (0,255), Rand (0,255), Rand (0,255)); Create a background
Imagesetpixel ($im, Rand ()%70,rand ()%30, $randcolor); Draw a single element
}
Imagepng ($im); To generate a PNG image
Imagedestroy ($im); Destroying images
?>
Note: PHP needs to be configured to perform related methods.
Operation Effect: