This article mainly introduces PHP instances that generate random strings and verification codes. if you need a php instance, you can refer to the PHP random number and verification code and articles on the Internet, there are only a few of them.
Simply do it yourself.
Let's start the php Tutorial in this section. the implementation of the following code can distinguish one get_code () and the other create_check_image (), and the output image is called directly after session () when the verification code is obtained, get_code () is OK. when using session, session_star () must be placed at the beginning.
The code is as follows:
The code is as follows:
Class RandCheckCode
{
/* Function name: get_code ()
* Function: obtain a random string.
* Parameters:
1. (int) $ length = 32 # random character length
2. (int) $ mode = 0 # random character type,
0 is a combination of uppercase and lowercase English letters and numbers, 1 is a number, 2 is a lowercase letter, 3 is a capital letter,
4: uppercase letters, 5: uppercase letters and numbers, 6: lowercase letters and numbers
* Return: the obtained string.
*/
Function get_code ($ length = 32, $ mode = 0) // Obtain the random verification code function
{
Switch ($ mode)
{
Case '1 ':
$ Str = '000000 ';
Break;
Case '2 ':
$ Str = 'abcdefghijklmnopqrstuvwxy ';
Break;
Case '3 ':
$ Str = 'abcdefghijklmnopqrstuvwxy ';
Break;
Case '4 ':
$ Str = 'abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxy ';
Break;
Case '5 ':
$ Str = 'abcdefghijklmnopqrstuvwxyz1234567890 ';
Break;
Case '6 ':
$ Str = 'abcdefghijklmnopqrstuvwxyz1234567890 ';
Break;
Default:
$ Str = 'abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz1234567890 ';
Break;
}
$ Checkstr = '';
$ Len = strlen ($ str)-1;
For ($ I = 0; $ I <$ length; $ I ++)
{
// $ Num = rand (0, $ len); // generates a random number between 0 and $ len.
$ Num = mt_rand (0, $ len); // generates a random number between 0 and $ len.
$ Checkstr. = $ str [$ num];
}
Return $ checkstr;
}
/** Function name: create_check_image ()
Function: generates an image of the verification code.
Parameter: $ checkcode: verification code string
Return value: returns the image.
*/
Function create_check_image ($ checkcode) // Generate
{
$ Im = imagecreate (); // generate an image
$ Black = imagecolorallocate ($ im, 0, 0); // background color
$ White = imagecolorallocate ($ im, 255,255,255); // foreground color
$ Gray = imagecolorallocate ($ im, 200,200,200 );
Imagefill ($ im, 30, 30, $ gray); // The coordinates of the $ im image are 30, 30 (the upper left corner of the image is 0, 0) fill the area with the $ gray color (that is, the adjacent points will be filled with the same color as the 30, 30 points)
Imagestring ($ im, 5, 8, 3, $ checkcode, $ white ); // use the $ white color to draw the string $ checkcode to the 8, 3 coordinates of the image represented by $ im (this is the coordinate of the upper left corner of the string, the upper left corner of the entire image is 0, 0), and 5 is the font size, the font can only be 1, 2, 3, 4, or 5. the built-in font is used.
For ($ I = 0; I I <120; $ I ++)
{
$ Randcolor = imagecolorallocate ($ im, rand (0,255), rand (0,255), rand (0,255 ));
Imagesetpixel ($ im, rand () % 70, rand () % 30, $ randcolor); // use $ randcolor on $ im images at (rand () % 70, rand () % 30) coordinate (the upper left corner of the image is 0, 0 ).
}
Header ("Content-type: image/png ");
Imagepng ($ im); // output the image to a browser or file in PNG format
Imagedestroy ($ im); // destroy the image $ im
}
}
/*
$ Randcode = new RandCheckCode ();
$ Checkstring = $ randcode-> get_code (5, 7 );
$ Image = $ randcode-> create_check_image ($ checkstring );
Echo $ image;
*/
?>