PHP generates image verification codes and click to switch instances

Source: Internet
Author: User
This article mainly introduces how to generate an image Verification Code instance in PHP and click switch (not clear? For more information, see

This article mainly introduces how to generate an image Verification Code instance in PHP and click switch (not clear? For more information, see

The following figure shows the effect:

Now let's take a look at the PHP code.

The Code is as follows:


<? Php

Session_start ();
Function random ($ len ){
$ Srcstr = "1a2s3d4f5g6hj8k9qwertyupzxcvbnm ";
Mt_srand ();
$ Strs = "";
For ($ I = 0; $ I <$ len; $ I ++ ){
$ Strs. = $ srcstr [mt_rand (0, 30)];
}
Return $ strs;
}

// Randomly generated string
$ Str = random (4 );

// The image width of the Verification Code
$ Width = 50;

// Height of the Verification Code Image
$ Height = 25;

// Declare the image format of the layer to be created
@ Header ("Content-Type: image/png ");

// Create a Layer
$ Im = imagecreate ($ width, $ height );

// Background color
$ Back = imagecolorallocate ($ im, 0xFF, 0xFF, 0xFF );

// Blur the vertex color
$ Pix = imagecolorallocate ($ im, 187,230,247 );

// Font color
$ Font = imagecolorallocate ($ im, 41,163,238 );

// Plot the vertices of fuzzy match.
Mt_srand ();
For ($ I = 0; I I <1000; $ I ++ ){
Imagesetpixel ($ im, mt_rand (0, $ width), mt_rand (0, $ height), $ pix );
}

// Output characters
Imagestring ($ im, 5, 7, 5, $ str, $ font );

// Output rectangle
Imagerectangle ($ im, 0, 0, $ width-1, $ height-1, $ font );

// Output image
Imagepng ($ im );

Imagedestroy ($ im );

$ Str = md5 ($ str );

// Select cookie
// SetCookie ("verification", $ str, time () + 7200 ,"/");

// Select Session
$ _ SESSION ["verification"] = $ str;
?>

Next, you just need to call it on the page:

The Code is as follows:



If you want to achieve "cannot see clearly? For a different effect, add the following JS to the page.

The Code is as follows:


Function changing (){
Document. getElementById ('checkpic '). src = "/images/checkcode. php? "+ Math. random ();
}

,

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.