Php+javascript to create a _php instance of code sharing with hints

Source: Internet
Author: User
Tags rand

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>
&LT;TD ><div align= "Right" > Verification Code:</div></td>
&LT;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>
&LT;TD align= "center" valign= "Middle" ><script>yzm () </script></td>
&LT;TD ><a href= "Javascript:code ()" style= "Text-decoration:none" > Can't see clearly </a></td>
&LT;TD width= "align=" "Center" ><div id= "yzm1" ><font color= "#999999" > Please enter the verification code &LT;/FONT&GT;&LT;/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:

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.