A simple PHP Verification Code Generation Scheme

Source: Internet
Author: User

// Before running, your computer needs to be able to run the PHP environment. Below are some of my practices

// This is a running page

 

// Show.html --- name

 

--------------------------------

<! Doctype HTML public "-// W3C // dtd html 4.01 transitional // en" "http://www.w3.org/TR/html4/loose.dtd">
<HTML>
<Head>
<Meta http-equiv = "Content-Type" content = "text/html; charset = UTF-8">
<Title> insert title here </title>
</Head>
<Body>
// an IMG tag used to display the verification code
<Span id = "change_code" style = "cursor: Pointer"> invisible </span> // click the key

// Extremely simple JSCode, It will be easier to use jquery

<SCRIPT type = "text/JavaScript">
Showval (); // run the verification code output once.

Document. getelementbyid ('change _ Code'). onclick = showval; // click the event to call

Function showval () {// Click Event
VaR numstr = "";
For (I = 0; I <4; I ++ ){
Num = math. Round (math. Random () * 10); // random 4-digit
Numstr + = num;
}
Document. getelementbyid ('code'). src = 'valcode. php? Num = '+ numstr; // import to SRC of IMG
}
</SCRIPT>

</Body>

</Html>

-----------------------------------------------------------------
// The above explanation is quite simple. The following is the main PHP code. It is the same as the previous article, but it has changed a little bit. Below is all the code
// The interpretation of the Code is the same as that of the previous chapter, so I didn't elaborate on it. I just explained it in part.

// Valcode. php // This is the name

 

-------------------------------------------------------------

<? PHP

Header ("Content-Type: image/PNG"); // header output.
$ Width = 80;
$ Height = 42;
$ Numimage = imagecreate ($ width, $ height );
Imagecolorallocate ($ numimage, 240,240,240 );

$ Num = $ _ Get ['num']; // ----------------then, the SRC of the last line in show.html contains a num, which can be obtained using the get method.

For ($ I = 0; $ I <strlen ($ num); $ I ++)
{
$ X = mt_rand (0, 10) + $ width * $ I/4;
$ Y = mt_rand (2, $ height/3 );
$ Color = imagecolorallocate ($ numimage, mt_rand (0,255), mt_rand (0,255), mt_rand (0,255 ));
Imagestring ($ numimage, 5, $ X, $ y, $ num {$ I}, $ color );
}

$ Color = imagecolorallocate ($ numimage, 100, 60,155 );
For ($ I = 0; I I <200; $ I ++ ){
Imagesetpixel ($ numimage, mt_rand (0, 80), mt_rand (0, 42), $ color );
}
Imagepng ($ numimage );
Imagedestroy ($ numimage );

?>

----------------------------------------------------------

 

Now you can try it.

 

 

 

 

 

 

 

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.