标签:
1 <?php 2 3 4 5 public function genToken($len = 32, $md5 = true) { 6 # Seed random number generator 7 # Only needed for PHP versions prior to 4.2 8 mt_srand((double) microtime() * 1000000); 9 # Array of characters, adjust as desired 10 $chars = array( 11 ‘Q‘, ‘@‘, ‘8‘, ‘y‘, ‘%‘, ‘^‘, ‘5‘, ‘Z‘, ‘(‘, ‘G‘, ‘_‘, ‘O‘, ‘`‘, 12 ‘S‘, ‘-‘, ‘N‘, ‘<‘, ‘D‘, ‘{‘, ‘}‘, ‘[‘, ‘]‘, ‘h‘, ‘;‘, ‘W‘, ‘.‘, 13 ‘/‘, ‘|‘, ‘:‘, ‘1‘, ‘E‘, ‘L‘, ‘4‘, ‘&‘, ‘6‘, ‘7‘, ‘#‘, ‘9‘, ‘a‘, 14 ‘A‘, ‘b‘, ‘B‘, ‘~‘, ‘C‘, ‘d‘, ‘>‘, ‘e‘, ‘2‘, ‘f‘, ‘P‘, ‘g‘, ‘)‘, 15 ‘?‘, ‘H‘, ‘i‘, ‘X‘, ‘U‘, ‘J‘, ‘k‘, ‘r‘, ‘l‘, ‘3‘, ‘t‘, ‘M‘, ‘n‘, 16 ‘=‘, ‘o‘, ‘+‘, ‘p‘, ‘F‘, ‘q‘, ‘!‘, ‘K‘, ‘R‘, ‘s‘, ‘c‘, ‘m‘, ‘T‘, 17 ‘v‘, ‘j‘, ‘u‘, ‘V‘, ‘w‘, ‘,‘, ‘x‘, ‘I‘, ‘$‘, ‘Y‘, ‘z‘, ‘*‘ 18 ); 19 # Array indice friendly number of chars; 20 $numChars = count($chars) - 1; 21 $token = ‘‘; 22 # Create random token at the specified length 23 for ($i = 0; $i < $len; $i++) 24 $token .= $chars[mt_rand(0, $numChars)]; 25 # Should token be run through md5? 26 if ($md5) { 27 # Number of 32 char chunks 28 $chunks = ceil(strlen($token) / 32); 29 $md5token = ‘‘; 30 # Run each chunk through md5 31 for ($i = 1; $i <= $chunks; $i++) 32 $md5token .= md5(substr($token, $i * 32 - 32, 32)); 33 # Trim the token 34 $token = substr($md5token, 0, $len); 35 } 36 37 return $token; 38 } 39
标签:
原文地址:http://www.cnblogs.com/it80/p/4919906.html