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.
<?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;
*/
?>