HTML code:
Copy the Code code as follows:
Simple Verification Code
JS Code:
Copy the Code code as follows:
function Chkyzm (form) {//Verify the verification code
if (form.yzm.value== "") {
Yzm1.innerhtml= "x verification code cannot be empty";
}else if (form.yzm.value!=form.yzm2.value) {
Yzm1.innerhtml= "x Verification code input error";
}else{
Yzm1.innerhtml= "√ Verify code input is correct";
}
}
function Yzm () {//Generate verification code
var num1=math.round (Math.random () *1000000);//Random decimal amplification
var num=num1.tostring (). substr (0,4);//take 4-bit integers
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 the Code code as follows:
Header ("Content-type:image/png");
$im =imagecreate (66,22); Create a 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 where random characters are located
$size =mt_rand (6,9); Define a font for random characters
$authnum =substr ($_get[num], $i, 1); Get the verification code passed in the hyperlink
Imagestring ($im, $size, ($i *15), $str, $authnum, Imagecolorallocate ($im, Rand (0,250), Rand (0,250), Rand (0,250)));// Rand (0,500) the degree of ambiguity in numbers
}//Horizontal output string
for ($i =0; $i <200; $i + +) {//execute for loop, add blur background for 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); Generate PNG images
Imagedestroy ($im); Destroying images
?>
Note: PHP needs to be configured to perform related methods.
Operating effect: