PHP+javascript Make Authentication Code Source Sharing with Tips
- 2021-06-28 11:47:44
- OfStack
html code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title> Simple Authentication Code </title>
</head>
<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"> Invisibility </a></td>
<td width="150" align="center"><div id="yzm1"><font color="#999999"> Please enter the verification code </font></div></td>
</tr>
</table>
</form>
</body>
</html>
JS code:
function chkyzm(form){ // Verify Authentication Code
if(form.yzm.value==""){
yzm1.innerHTML="<font color=#FF0000> ×Authentication code cannot be empty </font>";
}else if(form.yzm.value!=form.yzm2.value){
yzm1.innerHTML="<font color=#FF0000> ×Authentication code input error </font>";
}else{
yzm1.innerHTML="<font color=green> _Verification code entered correctly </font>";
}
}
function yzm(){ // Generate Authentication Code
var num1=Math.round(Math.random()*1000000);// Random decimal amplification
var num=num1.toString().substr(0,4);// take 4 Bit Integer
var yzm2=document.getElementById("yzm2");
document.write("<img name=codeimg src=yzm.php?num="+num+"'>");
yzm2.value=num;
}
function code(){ // Reset Authentication 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:
<?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++){ // Definition 4 Bit Random Numbers
$str=mt_rand(1,5); // Defines where random characters are located Y coordinate
$size=mt_rand(6,9); // Define the font for 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)));//rand(0,500) Fuzziness of numbers
} // Horizontal Output String
for($i=0;$i<200;$i++){ // implement for Loop to add a blurred background to the Authentication Code
$randcolor=imagecolorallocate($im,rand(0,255),rand(0,255),rand(0,255)); // Create Background
imagesetpixel($im,rand()%70,rand()%30,$randcolor); // Draw a list 1 element
}
imagepng($im); // generate png image
imagedestroy($im); // Destroy Image
?>
Note: PHP needs to be configured to execute related methods.
Operation effect: