Php idea and implementation of generating SessionID and image verification code

Source: Internet
Author: User
The verification code is required for background login, and SessionID is required for foreground user tracking. of course, the default PHP will have a SessionID after the Session is enabled, but I need my own, and can be stored in the database, so I tried to construct the following functions. * ****** Generate SessionID ***** the basic idea is to use the verification code for background login in the current microsecond, and SessionID for foreground user tracking. of course, the default PHP has a SessionID after the Session is enabled, but I need my own SessionID and can store it in the database. then I tried to construct the following functions.

/****** 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 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 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: 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 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 () p, rand () 0, $ 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.

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.