Do the verification code to use, and then the function is encapsulated, using the time to set 2 parameters:
$str set the string to be collected, such as:
$str = ' Efasfgzsrhftjxjxjhsrth ';
Then the string generated in the function will be randomly fetched from the Efasfgzsrhftjxjxjhsrth;
The $codeLen sets the random string to be generated, and sets 5 to generate 5 random strings.
Principle: Random fetch of strings, concatenation of strings
Effect:
Code:
<?php//mt_rand get random number Mt_rand (min, max); $str = "ABCDEFGHIJKMNPQRSTUVWXYZ0123456789ABCDEFGHIGKLMNPQRSTUVWXYZ" ;//Set the randomly collected string $codelen= ' 5 ';//Set the generated random number of function Str_rand ($STR, $codeLen) { $rand = ""; for ($i =0; $i < $codeLen-1; $i + +) { $rand. = $str [Mt_rand (0, strlen ($str)-1)]; such as: The random number is : $str [+] } return $rand;} $code =str_rand ($str, $codeLen); Echo $code;
?>
PHP Learning Note: Universal random string generation function (already packaged)