<?php
function random($length) {
$hash = '';
$chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefghijklmnopqrstuvwxyz';
$max = strlen($chars) - 1;
PHP_VERSION < '4.2.0' && mt_srand((double)microtime() * 1000000);
for($i = 0; $i < $length; $i++) {
$hash .= $chars[mt_rand(0, $max)];
}
return $hash;
}
function random2($length, $numeric = 0) {
PHP_VERSION < '4.2.0' ? mt_srand((double)microtime() * 1000000) : mt_srand();
$seed = base_convert(md5(print_r($_SERVER, 1).microtime()), 16, $numeric ? 10 : 35);
$seed = $numeric ? (str_replace('0', '', $seed).'012340567890') : ($seed.'zZ'.strtoupper($seed));
$hash = '';
$max = strlen($seed) - 1;
for($i = 0; $i < $length; $i++) {
$hash .= $seed[mt_rand(0, $max)];
}
return $hash;
}
?>
四、最终方案:
思路:userid+秒+随机数。其中“userid+秒”10进制转64进制,缩减位数;
说明:
userid: 64进制最大值“ZZZZ”转换为十进制等于”16777215“,”ZZZ“转换为十进制最大值等于”262143“;
秒:设置自己的时间起点。
$less=time()-strtotime('2012-4-21′); 转换为64进制”1SpRe“,5位
$less=time()-strtotime('2013-3-21′); 转换为64进制”_jHY“;4位
随机数:使用random(3)生成3位随机数;
最终结果:
4位userid+4位秒+3位随机数=11位字符串。虽然与uniqid()结果看上去相似,但是强壮度有所提高。
五、十进制转64进制算法:
算法1:
复制代码 代码如下:
const KeyCode = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_$';
/**
* 将64进制的数字字符串转为10进制的数字字符串
* @param $m string 64进制的数字字符串
* @param $len integer 返回字符串长度,如果长度不够用0填充,0为不填充
* @return string
* @author 野马
*/
function hex64to10($m, $len = 0) {
$m = (string)$m;
$hex2 = '';
$Code = KeyCode;
for($i = 0, $l = strlen($Code); $i < $l; $i++) {
$KeyCode[] = $Code[$i];
}
$KeyCode = array_flip($KeyCode);
for($i = 0, $l = strlen($m); $i < $l; $i++) {
$one = $m[$i];
$hex2 .= str_pad(decbin($KeyCode[$one]), 6, '0', STR_PAD_LEFT);
}
$return = bindec($hex2);
if($len) {
$clen = strlen($return);
if($clen >= $len) {
return $return;
}
else {
return str_pad($return, $len, '0', STR_PAD_LEFT);
}
}
return $return;
}
/**
* 将10进制的数字字符串转为64进制的数字字符串
* @param $m string 10进制的数字字符串
* @param $len integer 返回字符串长度,如果长度不够用0填充,0为不填充
* @return string
* @author 野马
*/
function hex10to64($m, $len = 0) {
$KeyCode = KeyCode;
$hex2 = decbin($m);
$hex2 = str_rsplit($hex2, 6);
$hex64 = array();
foreach($hex2 as $one) {
$t = bindec($one);
$hex64[] = $KeyCode[$t];
}
$return = preg_replace('/^0*/', '', implode('', $hex64));
if($len) {
$clen = strlen($return);
if($clen >= $len) {
return $return;
}
else {
return str_pad($return, $len, '0', STR_PAD_LEFT);
}
}
return $return;
}
/**
* 将16进制的数字字符串转为64进制的数字字符串
* @param $m string 16进制的数字字符串
* @param $len integer 返回字符串长度,如果长度不够用0填充,0为不填充
* @return string
* @author 野马
*/
function hex16to64($m, $len = 0) {
$KeyCode = KeyCode;
$hex2 = array();
for($i = 0, $j = strlen($m); $i < $j; ++$i) {
$hex2[] = str_pad(base_convert($m[$i], 16, 2), 4, '0', STR_PAD_LEFT);
}
$hex2 = implode('', $hex2);
$hex2 = str_rsplit($hex2, 6);
foreach($hex2 as $one) {
$hex64[] = $KeyCode[bindec($one)];
}
$return = preg_replace('/^0*/', '', implode('', $hex64));
if($len) {
$clen = strlen($return);
if($clen >= $len) {
return $return;
}
else {
return str_pad($return, $len, '0', STR_PAD_LEFT);
}
}
return $return;
}
/**
* 功能和PHP原生函数str_split接近,只是从尾部开始计数切割
* @param $str string 需要切割的字符串
* @param $len integer 每段字符串的长度
* @return array
* @author 野马
*/
function str_rsplit($str, $len = 1) {
if($str == null || $str == false || $str == '') return false;
$strlen = strlen($str);
if($strlen <= $len) return array($str);
$headlen = $strlen % $len;
if($headlen == 0) {
return str_split($str, $len);
}
$return = array(substr($str, 0, $headlen));
return array_merge($return, str_split(substr($str, $headlen), $len));
}
$a=idate("U");
echo "\r\n<br />e:" . hex10to64($a);
echo "\r\n<br />e:" . hex64to10(hex10to64($a));
算法2:
复制代码 代码如下: