/****** Generate the session ID ******/
The basic idea is to get the time of the current microsecond, then generate a random number, add the random number and the current time, encrypt it, and finally extract the required length.
/*
Function Name: create_sess_id ()
Function: generate a random session ID.
Parameter: $ Len: the length of the session string. The default value is 32 bits, not lower than 16 bits.
Return Value: the ID of the returned session.
Function Author: heiyeluren
*/
Function create_sess_id ($ Len = 32)
{
// Check whether the submitted length is valid
If (! Is_numeric ($ Len) | ($ Len> 32) | ($ Len <16) {return ;}
// Obtain the microseconds of the current time
List ($ U, $ S) = explode ('', microtime ());
$ Time = (float) $ U + (float) $ S;
// Generate a random number
$ Rand_num = rand (100000,999 999 );
$ Rand_num = rand ($ rand_num, $ time );
Mt_srand ($ rand_num );
$ Rand_num = mt_rand ();
// Generate sessionid
$ Sess_id = MD5 (MD5 ($ time). MD5 ($ rand_num ));
// Intercept the specified sessionid.
$ Sess_id = substr ($ sess_id, 0, $ Len );
Return $ sess_id;
}
/****** Generate the verification code ******/
Idea: this idea is relatively simple. Considering uniqueness and randomness, we can extract a string from the session ID in the verification code, because our sessionid is unique.
/*
Function Name: create_check_code ()
Function: generate a Random verification code.
Parameter: $ Len: length of the verification code. The value must be no longer than 16 digits. The default value is 4 digits.
Return Value: return the verification code of the specified length.
Function Author: heiyeluren
*/
Function create_check_code ($ Len = 4)
{
If (! Is_numeric ($ Len) | ($ Len> 6) | ($ Len <1) {return ;}
$ Check_code = substr (create_sess_id (), 16, $ Len );
Return strtoupper ($ check_code );
}
/****** Image for generating the verification code ******/
This is something simple for PHP image programming. I made images and they are simple.
/*
Function Name: create_check_image ()
Function function: generates an image of the verification code.
Parameter: $ check_code: check code string, which is generally obtained by the create_check_code () function.
Return Value: returns the image.
Function Author: heiyeluren
*/
Function create_check_image ($ check_code)
{
// Generate an image
$ Im = imagecreate (65,22 );
$ Black = imagecolorallocate ($ im, 0, 0); // background color
$ White = imagecolorallocate ($ im, 255,255,255); // foreground color
$ Gray = imagecolorallocate ($ im, 200,200,200 );
Imagefill ($ im, 68, 30, $ gray );
// Print the four-digit integer verification code into the image
Imagestring ($ im, 5, 8, 3, $ check_code, $ white );
// Add interference pixels
For ($ I = 0; I I <200; $ I ++)
{
$ Randcolor = imagecolorallocate ($ im, Rand (0,255), Rand (0,255), Rand (0,255 ));
Imagesetpixel ($ im, Rand () % 70, Rand () % 30, $ randcolor );
}
// Output image
Header ("Content-Type: image/PNG ");
Imagepng ($ IM );
Imagedestroy ($ IM );
}
Note that When referencing the create_check_image () function, it must be in a separate file, because the output format is the image format when the output file header is mixed with other content, the image cannot be displayed. In addition, you can change the image generation function. For example, if you want to change the color, you can replace the watermark and the background color, at the same time, you must change the color of the Verification Code. Otherwise, the background and Verification Code are both black and cannot be displayed.