PHP instance _php instance of a class that generates random strings and Authenticode

Source: Internet
Author: User
Tags lowercase php tutorial rand

There are a lot of PHP random numbers and code codes with the article, the real application of the few.

Let's just get one yourself.

Start this section of the PHP tutorial, the following implementation of the Code, the main to be able to distinguish a get_code (), another create_check_image (), output image directly after the call, session () to take the verification code directly Get_code () on the OK, Incidentally, the Session_star () must be placed at the front of the session.

The code is as follows:

Copy Code code as follows:

<?php
Class Randcheckcode
{
/* Function Name: Get_code ()
* Function: Get random string
Parameters
1, (int) $length = #随机字符长度
2, (int) $mode = 0 #随机字符类型,
0 is written in English and numerals, 1 for digits, 2 for lowercase, 3 for uppercase letters,
4 for uppercase and lowercase, 5 for uppercase letters and digits, 6 for lowercase letters and numbers
* Return: the string obtained
*/
function Get_code ($length =32, $mode =0)//Get random Authentication code functions
{
Switch ($mode)
{
Case ' 1 ':
$str = ' 123456789 ';
Break
Case ' 2 ':
$str = ' abcdefghijklmnopqrstuvwxyz ';
Break
Case ' 3 ':
$str = ' abcdefghijklmnopqrstuvwxyz ';
Break
Case ' 4 ':
$str = ' abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz ';
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);//Generate a random number between 0 and $len
$num =mt_rand (0, $len);//Generate a random number between 0 and $len
$checkstr. = $str [$num];


}
return $checkstr;
}

/** function Name: Create_check_image ()
Function: Create a picture of a checksum code
Parameters: $checkcode: Checksum code string
Return value: Returns the picture
*/
function Create_check_image ($checkcode)//produces a
{
$im =imagecreate (65,22);//produce a picture
$black =imagecolorallocate ($im, 0,0,0);//Background color
$white =imagecolorallocate ($im, 255,255,255);//Foreground color
$gray =imagecolorallocate ($im, 200,200,200);
Imagefill ($im, 30,30, $gray)//The $im of the coordinates 30,30 (the upper-left corner of the image is 0,0) with the $gray color to perform the area fill (that is, the same color as the 30, 30 dots and adjacent points will be filled)

Imagestring ($im, 5,8,3, $checkcode, $white)///$white The string $checkcode to the 8, 3 coordinates of the image represented by $im (this is the upper-left corner of the string, the upper-left corner of the image is 0,0), 5 is the font size, the font can only be 1,2,3,4 or 5, using the built-in font
For ($i =0 $i <120; $i + +)
{
$randcolor =imagecolorallocate ($im, Rand (0,255), Rand (0,255), Rand (0,255));
Imagesetpixel ($im, Rand ()%70,rand ()%30, $randcolor);//on $im image with $randcolor color in (rand ()%70,rand ()%30) coordinates (the upper-left corner of the image is 0,0) Draw a dot on the
}
Header ("Content-type:image/png");
Imagepng ($im);//Export the image to a browser or file in PNG format
Imagedestroy ($im);//Destroy 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.