您的位置:首页 > 编程语言 > PHP开发

php web下订单编号的生成(隐藏订单上面的统计信息)

2014-10-20 18:26 507 查看
因为项目需求,需要生成订单编号的东西,而订单编号上面的统计信息有必要隐藏起来,而考虑到项目中很多地方也需要生成唯一随机字串,使用时间一秒钟最多应用毫秒数来生成1000个唯一编号,而使用过程中,订单编号可能多,使我有了下面的想法:

1、所有串都是26个字母+10个数字共36个不同字符来组成;

2、日期可以这么考虑,十年以上的数据存留已经够用了,不知道十年后公司还存不存在,所以36个来代表年份够用了,直接取余,月、日、小时也足够用36个字符来标识,同样取余;

3、分钟有60分,超过36,用两位来标识:十位、各位,秒同样!这样一共占据了8位;

4、剩下6位用累加统计来表示,使用共享内存进行累加;

上面4条就能保证在一分钟内,能生成99万个不同的字串,并且36位字串的组成可以随机(后面代码将看到效果);如果需求的字串长于14位,则在末尾加上利用随机数读取的字符串,这样更能保证获得的字串唯一;

直接上代码

<?php
class UniqueTools
{
private static $key = 39527; // the key of shm
private static $memsize = 8; // size of shm by byte
private static $perm = 0666; // promission
private static $offset = 0; // offset of shm,0 is the first address;
private static $_instance;

private static $end = 102310;
private static $str = "ABC1DE9F2GH3IJK0LM4NOP8QR5STUV6WXY7Z";
private static $str1 = "G5D2C7B4J1F6A8H3I9E0ZYXWVUTSRQPONMLK";

private function __construct()
{
}

private function __clone()
{
/*覆盖__clone()方法,禁止克隆*/
}

public static function getInstance()
{
if (!(self::$_instance instanceof self)) {
self::$_instance = new self();
}
return self::$_instance;
}

function getParam(array $arr, $key, $default = null, $trimValue = true)
{
if (!isset($arr[$key]) || strlen($arr[$key]) < 1)
return $default;

$value = $arr[$key];

if ($trimValue)
$value = trim($value);

return $value;
}

private function getShmid()
{
try {
$semid = sem_get(0xee5, 1, 0666);
sem_acquire($semid);
$shm_data = self::$end;
$shm_id = shmop_open(Self::$key, "w", 0, 0);
/*
打开key为<span style="font-family: Arial, Helvetica, sans-serif;">Self::$key</span>的共享内存,第二个参数w表示以读写方式打开,打开已存在的共享内存,第三个和第四个参数必须是0
*/
if (!$shm_id) {
$shm_id = shmop_open(Self::$key, "c", Self::$perm, Self::$memsize);
shmop_write($shm_id, self::$end, 0); // write the data to the shm;
} else {

$shm_data = shmop_read($shm_id, 0, 8); /* 从共享内存里面读取8字节的数据,第二个参数是偏移地址,0表示共享内存的起始地址   */

$shmwback = (int)$shm_data + 1;
if ($shmwback > 999999) {
$shmwback = self::$end;
}
shmop_write($shm_id, $shmwback, 0);
}

shmop_close($shm_id); //close shm
sem_release($semid);
return (int)$shm_data;
} catch (Exception $ex) {
$shm_id = shmop_open($this->$key, "c", Self::$perm, Self::$memsize);
shmop_write($shm_id, self::$end, 0); // write the data to the shm;
sem_release($semid);
return $shm_id;
}
}

public function getUniqid($length = 14)//min length = 14
{

$len = 0;
if (func_num_args() == 1 && is_int($length)) {
$len = $length - 14;
}

$str = self::$str;
$str1 = self::$str1;
$time = time();
$timestr = $str[(int)date('Y', $time) % 26]
. $str1[(int)date('m', $time) % 26 + 12]
. $str[(int)date('d', $time) % 36 + 2]
. $str1[(int)date('H', $time) % 25 + 2]
. $str1[(int)(date('i', $time) / 10) + 10]
. $str[(int)date('i', $time) % 10 + 20]
. $str[(int)(date('s', $time) / 10) + 8]
. $str1[(int)date('s', $time) % 10 + 18];//length = 8;

$shmid = $this->getShmid() . 'A';
$n = date('s', $time) % 26;
$result = $timestr . $str1[$shmid[0] + $n] . $str1[$shmid[1] + $n] . $str1[$shmid[2] + $n] . $str1[$shmid[3] + $n] . $str1[$shmid[4] + $n] . $str1[$shmid[5] + $n];

$len = $len < 0 ? 0 : $len;
if ($len > 0) {
$s = "";
for ($i = 0; $i < $len; $i++) {
$s .= $str[mt_rand(0, strlen($str) - 1)];
}
$result = $result . $s;
}
return $result;
}

/*
* create a 32+bit unique id
* @param true:32 bits unique string
*        false: longer then 32 bits unique strings
*/

public function get32BitUniqid($base64 = false)
{
$prestr = $this->getUniqid();
$s = md5($prestr);
if (!$base64) {
return $s;
}
$s = base64_encode($s);
return $s;
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: