PHP Generate SessionID and picture check code ideas and implementation code _php tutorial

Source: Internet
Author: User
Tags session id
/****** generates session ID ******/
The basic idea: is to take the current microsecond time to obtain, and then generate a random number, the random number and the current time after the addition of encryption, and finally intercept the required length
/*
Function name: create_sess_id ()
Function: Generates a random session ID
Parameters: $len: The length of the session string is required, default is 32 bits, not less than 16 bits
Return Value: Return session ID
Function Author: heiyeluren
*/
function create_sess_id ($len =32)
{
Verify the length of the commit is legal
if (!is_numeric ($len) | | ($len >32) | | ($len <16)) {return;}
Gets the current time in microseconds
List ($u, $s) = explode (' ', Microtime ());
$time = (float) $u + (float) $s;
Generate a random number
$rand _num = rand (100000, 999999);
$rand _num = rand ($rand _num, $time);
Mt_srand ($rand _num);
$rand _num = Mt_rand ();
Produce SessionID
$sess _id = MD5 (MD5 ($TIME). MD5 ($rand _num));
Intercept a SessionID that specifies the length required
$sess _id = substr ($sess _id, 0, $len);
return $sess _id;
}


/****** Generate check Code ******/
Idea: This idea is relatively simple, because consider uniqueness and randomness, our check code on the session ID to intercept a string, because our SessionID is fully considered unique.

/*
Function name: Create_check_code ()
Function: Generates a random check code
Parameter: $len: Requires the length of the check code, please do not longer than 16 bits, the default is 4 bits
Return value: Returns a check 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 (), $len);
Return Strtoupper ($check _code);
}


/****** generate a picture of the checksum code ******/

This is some of the more simple PHP graphics programming things up, I made the pictures and simple.

/*
Function name: Create_check_image ()
Function: Produces a picture of a checksum code
Parameters: $check _code: Check code string, generally by the Create_check_code () function to obtain
Return value: Returns the picture
Function Author: heiyeluren
*/
function Create_check_image ($check _code)
{
produce a picture
$im = Imagecreate (65,22);
$black = Imagecolorallocate ($im, 0,0,0); Background color
$white = Imagecolorallocate ($im, 255,255,255); Foreground color
$gray = Imagecolorallocate ($im, 200,200,200);
Imagefill ($im, 68,30, $gray);

Drawing a four-bit integer verification code into a picture
Imagestring ($im, 5, 8, 3, $check _code, $white);
adding interfering pixels
for ($i =0; $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);
}

Here we should note that when referring to the Create_check_image () function, it must be in a separate file, because the output file header when the output format is the image format, the inclusion of other content, will cause the picture can not be displayed. In addition, the picture genetic function, you can change, for example, you want to modify the color, then you put the foreground color and background color of the generated position for a change, then the color is not the same, but also to check the color of the code, otherwise the background and check code is black will not show out.

http://www.bkjia.com/PHPjc/319940.html www.bkjia.com true http://www.bkjia.com/PHPjc/319940.html techarticle /****** generates session ID ******/basic idea: is to take the current microsecond time to obtain, and then generate a random number, the random number and the current time after the addition of encryption, and finally intercepted ...

  • 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.