Home > Article > Backend Development > Ideas and implementation of generating SessionID and image verification code in php_PHP tutorial
A verification code is required for backend login, and SessionID is required for user tracking in the frontend. Of course, the default PHP will have a SessionID after opening the Session, but I need my own and can be stored in the database, then I will I tried it and constructed the following function.
/****** Generate Session ID ******/
The basic idea is to obtain the current microsecond time, then generate a random number, add the random number to the current time and encrypt it, and finally intercept the required The length of
/*
Function name: create_sess_id()
Function function: Generate a random session ID
Parameters: $len: The length of the session string is required, the default is 32 bits, no Less than 16 digits
Return value: Return session ID
Function heiyeluren
*/
function create_sess_id($len=32)
{
// Verify whether the submitted length is legal
if( !is_numeric($len) || ($len>32) || ($len<16)) { return; }
// Get the microseconds of the current time
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();
// Generate SessionID
$sess_id = md5( md5($time). md5($rand_num) );
//Intercept the SessionID of the specified required length
$sess_id = substr($sess_id, 0, $len);
return $sess_id ;
}
/****** Generate verification code ******/
Idea: This idea is relatively simple, because considering uniqueness and randomness, our check code is to intercept a string from the Session ID That's ok, because our SessionID is fully considered to be unique.
/*
Function name: create_check_code()
Function function: Generate a random check code
Parameter: $len: The length of the check code required, please not be longer than 16 bits, missing The province is 4 digits
Return value: Returns the check 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);
}
/****** Picture of generating verification code ******/
This is some relatively simple PHP image programming stuff. The pictures I made are simple.
/*
Function name: create_check_image()
Function function: generate a check code image
Parameters: $check_code: Check code string, generally obtained by the create_check_code() function