Idea and implementation of php native 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 use the verification code for background login 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 structure 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 author: heiyeluren
*/
Function create_sess_id ($ len = 32)
{
// Check whether the submission length is correct
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 ******/
Train of thought: this train of thought is comparatively simple. Considering the unique uniqueness and randomness, we can extract a string from the Session ID for the verification code, because our SessionID fully considers unique and unique.

/*
Function name: create_check_code ()
Function: generate a random verification code.
Parameter: $ len: length of the verification code. do not be good at 16 bits. the default value is 4 bits.
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 );
}


/****** Picture of the natural verification code ******/

This is something more simple PHP image programming, I made pictures and 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 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 );

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.