php唯一串生成方式 uniqid …
- 高并发32位唯一字符串;
- 可变唯一字符串;
- 订单号唯一串;
- 低频13位唯一串;
/**
* 生成指定位数随机字符串(返回字符串长度=$length*2)
* @param int $length
* @return false|string
*/
public static function getRandomString($length = 6)
{
$pool = '0123456789abcdefghijklmnopqrstuvwxyz';
if (function_exists('openssl_random_pseudo_bytes')) {
$bytes = openssl_random_pseudo_bytes($length, $cstrong);
if ($bytes === false) {
return substr(str_shuffle(str_repeat($pool, 5)), 0, $length * 2);
}
return bin2hex($bytes);
}
return substr(str_shuffle(str_repeat($pool, 5)), 0, $length * 2);
}
生成初始密码
$salt = substr(uniqid(rand()),-6);
$key = 'xx.xxx.com';
$pwd = md5(md5('123456').$salt.$key);
生成唯一id的几种方法
uniqid(); 13位字符串
uniqid('',true); 23位字符串
md5(uniqid(mt_rand(1,10000)));32位字符串
md5(uniqid(md5(microtime(true)),true)); 32位字符串
md5( uniqid( md5(microtime(true)),true ) . mt_rand(1,10000) );32位字符串
session_create_id()
//创建24位唯一值 14+6+4 20140507115523 123456 1234
date('YmdHis').substr(microtime(),2,6).mt_rand(1000,9999);
-
- uniqid() 13位字符串
$units = array();
for ($i = 0; $i < 3; $i++) {
$units[] = uniqid();
}
var_export($units);
array (
0 => '5fa799bee9f7e',
1 => '5fa799bee9f95',
2 => '5fa799bee9f96',
)
-
- uniqid() 23位字符串
$units = array();
for ($i = 0; $i < 3; $i++) {
$units[] = uniqid('', true);
}
var_export($units);
array (
0 => '5fa79a8371e2b9.16693379',
1 => '5fa79a8371e573.86696425',
2 => '5fa79a8371e5a8.44085231',
)
-
- uniqid() 32位字符串
$units = array();
for ($i = 0; $i < 3; $i++) {
$units[] = md5(uniqid(mt_rand(), true));
// $units[] = md5(uniqid(md5(microtime(true)),true))
}
var_export($units);
array (
0 => '56d06914ee44c0f568be745edf1410de',
1 => '914a38fe5a2b3124bc42593b39c12a22',
2 => 'fa4853979a3e6d19748c9b354f034d49',
)
参考链接