PHP instance of the class for generating random strings and verification codes _ php instance

Source: Internet
Author: User
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;
*/

?>

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.