php订单号方案,重复的可能性要很低很低很低,大家说说自己的方案。
php订单号方案,重复的可能性要很低很低很低,大家说说自己的方案。
回复内容:
php订单号方案,重复的可能性要很低很低很低,大家说说自己的方案。
我的算法是:
买家UID+商家UID+年月日+三位随机数
大家觉得怎么样?
我来说下对京东ID编号的看法,两年前京东订单就突破2亿,现在应该超过四个亿了,十位订单最长才99亿,从这个占比来说,京东的订单号应该不是简单某种随机算法做出来的,如果是随机算法做出来的,肯定要做二次检验,ID是否已存在,如果存在就再生成新的。根据我分析自己在京东购买东西实际编号,丫就是按从小到大生成的。
我估计有个外置ID生成服务器,要不然肯定抗不住双11这种,比如可以有一台专门服务器分多个进程,每个进程负责10万个编号分配,没有了就再申请,进程间的编号独立,或者简单点每台应用服务器就有个专属ID区域。
我们这的方式就是:时间戳
+ 机器编号
+ 0-1000循环编号
首先时间戳是不会重复的,好像这样就保证了不重复吧。
我看了下淘宝的订单和账户有关。 同一个账户对同一个商家的订单号尾数好像。。是一样的
有反馈下面代码,不靠谱,冲撞率极高,这里就仅做学习,不推荐正式环境使用了
<code> /** * * uniqid - 官方是这样说的: * Gets a prefixed unique identifier based on the current time in microseconds. */ function build_order_no() { return date('Ymd').substr(implode(NULL, array_map('ord', str_split(substr(uniqid(), 7, 13), 1))), 0, 8); }</code>
补充:来自ECSHOP订单号生成函数:/includes/lib_order.php文件中的get_order_sn()
<code>/** * 得到新订单号 * @return string */ function build_order_no() { /* 选择一个随机的方案 */ mt_srand((double) microtime() * 1000000); return date('Ymd') . str_pad(mt_rand(1, 99999), 5, '0', STR_PAD_LEFT); }</code>
from :http://levi.cg.am/archives/3373
有一个东西叫 UKG 专门来干这个事情的。可以了解下:https://github.com/liexusong/ukg
我觉得uniqid就行
http://php.net/manual/zh/function.uniqid.php
可以 参考一下 ecshop 的订单生成机制
<code>return date('Ymd') . str_pad(mt_rand(1, 99999), 5, '0', STR_PAD_LEFT); </code>
$str = date('ymd') . substr(implode(null, array_map('ord', str_split(substr(uniqid(), 7, 13), 1))), 0, 8);
或者
$yCode = array('A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J');
$orderSn = $yCode[intval(date('Y')) - 1970] . strtoupper(dechex(date('m'))) . date('d') . substr(time(), -5) . substr(microtime(), 2, 5) . sprintf('%04d%02d', rand(1000, 9999), rand(0, 99));
上面的方案生成的订单号比较长,京东的十位订单号是怎么生成和保证不重复的呢?
订单编号在什么时候生成?怎样避免生成重复的?事务安全?存储过程?
sys_guid
为什么我想用hash做订单号……
<code>/* * 得到唯一的订单编号 * @params null * @return string 订单编号 */ /** preterchan **/ public function gen_id($n=0, $data_format='ymdHi') { $i = rand(0,99999); do{ if(99999==$i){ $i=0; } $i++; $order_id = date($data_format).str_pad($i,5,'0',STR_PAD_LEFT); if ($n) $order_id .= $n; $row = $this->db->selectrow('SELECT order_id from orders where order_id ='.$order_id); }while($row); return $order_id; } </code>
生成出的数字转换成36进制(数字+26个字母),可以缩短长度
比如 :
<code> //会员号 + 秒 + 微秒 + 随机 $mid=118914; list($usec, $sec) = explode(" ", microtime()); $rand=rand(1000,9999); echo strtoupper(base_convert($mid,10,36).base_convert($sec,10,36).base_convert($usec*10000000,10,36).base_convert($rand,10,36)); //2JR6NE7JXQ5SPY85BY </code>
如果没有别的前提要求,自增长就足够了...
我之前一个项目的要求是不能让用户看出是怎么生成的,表面上看起来要完全随机
那么加时间戳也会被看出来,使用用户uid也会被看出来,随机生成又有可能出现重复
最后是这样做的
<code> $time_order=time(); list($usec, $sec)=explode(" ", microtime()); $orderid=substr($uid*rand(11,55),0,5); $orderid.=date('ymdHis',$sec).ceil($usec*10); $orderid=substr($orderid,0,18); </code>

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

If you are an experienced PHP developer, you might have the feeling that you’ve been there and done that already.You have developed a significant number of applications, debugged millions of lines of code, and tweaked a bunch of scripts to achieve op

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

A string is a sequence of characters, including letters, numbers, and symbols. This tutorial will learn how to calculate the number of vowels in a given string in PHP using different methods. The vowels in English are a, e, i, o, u, and they can be uppercase or lowercase. What is a vowel? Vowels are alphabetic characters that represent a specific pronunciation. There are five vowels in English, including uppercase and lowercase: a, e, i, o, u Example 1 Input: String = "Tutorialspoint" Output: 6 explain The vowels in the string "Tutorialspoint" are u, o, i, a, o, i. There are 6 yuan in total

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

What are the magic methods of PHP? PHP's magic methods include: 1.\_\_construct, used to initialize objects; 2.\_\_destruct, used to clean up resources; 3.\_\_call, handle non-existent method calls; 4.\_\_get, implement dynamic attribute access; 5.\_\_set, implement dynamic attribute settings. These methods are automatically called in certain situations, improving code flexibility and efficiency.
