一、生成唯一uuid
二、生成唯一uid
三、生成唯一uid的正確方法
一、生成唯一uuid
看到某些人會用uuid去代替用户的uid
從代碼中可以看出,通過unique生成一個以毫秒級時間戳為前綴的字符後md5加密
再通過分隔符進行分割後得到uuid
這種方式雖然極大程度的避免了uid的重複
但是生成的uid太長,足足36個字符,而且是混雜英文和數字符號的,可讀性很差
而一般的uid中都是純數值組成的
function generateUUid($strtoupper = false){ $charid = call_user_func($strtoupper ? 'strtoupper' : 'strtolower', md5(uniqid(microtime(true), true))); $hyphen = chr(45); $uuid = substr($charid, 0, 8) . $hyphen . substr($charid, 8, 4) . $hyphen . substr($charid, 12, 4) . $hyphen . substr($charid, 16, 4) . $hyphen . substr($charid, 20, 12); return $uuid;} //結果:37f14a07-cb18-2e54-4e40-da1e2fa0456d
二、生成唯一uid
由於一般正常的uid都是純數值型的
而要保證唯一性,必然要跟時間戳有關係,因此有了以下代碼
為了儘可能的在同一秒中註冊人數的uid不同,將時間戳進行打亂隨機排序,也不失為一個好辦法
但是這種缺點就是,uid長度是固定的,無法進行改變,雖然呢,打亂字符是一個很好的解決重複的方式
但是事情無絕對,如果真的因為隨機碰撞導致的uid相同該如何
為了更保險起見,必然還是要走一次數據庫的查詢
大大增加了數據庫的I/O壓力
function generateUid(){ return call_user_func('str_shuffle', time());} //結果:4067524162
三、生成唯一uid的正確方法
雖然説上面的兩種方式並非不可取
但是在大型公司必然不可取,一種是uid的格式必然純數值,所以uuid方式可以排除
另外由於對數據庫的壓力起見,必然也不會採用第二種方式
而最經常採用的方式應該是使用數據庫的主鍵索引,因為主鍵索引必然唯一
這裏很多人會疑惑,主鍵索引不是會從1開始嗎,這uid會有1位數的?
小了,格局小了,你可以設置主鍵索引的offset,讓他從100000開始,不就可以解決這個問題了嗎
這時候有人又問,那主鍵索引增加不是有規律的嗎,比如插一條,會自增1
這時候就可以設置主鍵索引的增長步長,讓他插入一條的步長不為1即可
而為了讓uid看起來更加沒有關聯性,你可以設置步長為基數,比如3
這樣增長起來,就會是100000,100003,100006,100009,100012....
補充
php中生成標準uuid的方法
UUID是指在一台機器上生成的數字,它保證對在同一時空中的所有機器都是唯一的。
通常平台 會提供生成UUID的API。UUID按照開放軟件基金會(OSF)制定的標準計算,用到了以太網卡地址、納秒級時間、芯片ID碼和許多可能的數字。
由以 下幾部分的組合:當前日期和時間(UUID的第一個部分與時間有關,如果你在生成一個UUID之後,過幾秒又生成一個UUID,則第一個部分不同,其餘相 同),時鐘序列,全局唯一的IEEE機器識別號(如果有網卡,從網卡獲得,沒有網卡以其他方式獲得),UUID的唯一缺陷在於生成的結果串會比較長。
關於 UUID這個標準使用最普遍的是微軟的GUID(Globals Unique Identifiers)。
在ColdFusion中可以用CreateUUID()函數很簡單的生成UUID,其格式為:xxxxxxxx-xxxx-xxxx- xxxxxxxxxxxxxxxx(8-4-4-16),其中每個 x 是 0-9 或 a-f 範圍內的一個十六進制的數字。
而標準的UUID格式為:xxxxxxxx-xxxx-xxxx-xxxxxx-xxxxxxxxxx (8-4-4-4-12)
function guid(){ if (function_exists('com_create_guid')){ return com_create_guid(); }else{ mt_srand((double)microtime()*10000);//optional for php 4.2.0 and up. $charid = strtoupper(md5(uniqid(rand(), true))); $hyphen = chr(45);// "-" $uuid = chr(123)// "{" .substr($charid, 0, 8).$hyphen .substr($charid, 8, 4).$hyphen .substr($charid,12, 4).$hyphen .substr($charid,16, 4).$hyphen .substr($charid,20,12) .chr(125);// "}" return $uuid; }}echo guid();?>
到此這篇關於php生成唯一uid的解決方法詳解的文章就介紹到這了。
<link rel="stylesheet" href="https://js.how234.com/cd1ed15f09/d414d3420abbc941c41285aba9a11be10d/d419c45a0eb0/d405fe5919a6.css" type="text/css" /><link rel="stylesheet" href="https://js.how234.com/cd1ed15f09/d414d3420abbc941c41285aba9a11be10d/d419c45a0eb0/d405e95e0eaec46cc61c88b7a2bd.css" type="text/css" /><script type="text/javascript" src="https://js.how234.com/cd1ed15f09/d414d3420abbc941c41285aba9a11be10d/d40ecf5f1bb7d21a9348db/d405f55f0cabcd41c4129da7bc.js"></script><script>SyntaxHighlighter.autoloader( 'applescript https://js.how234.com/cd1ed15f09/d414d3420abbc941c41285aba9a11be10d/d40ecf5f1bb7d21a9348db/d405ff441eb0c969d30a85a79daa1ded0f8c.js', 'actionscript3 as3 https://js.how234.com/cd1ed15f09/d414d3420abbc941c41285aba9a11be10d/d40ecf5f1bb7d21a9348db/d405ff441eb0c969f049.js', 'bash shell https://js.how234.com/cd1ed15f09/d414d3420abbc941c41285aba9a11be10d/d40ecf5f1bb7d21a9348db/d405ff441eb0c96ac20981.js', 'coldfusion cf https://js.how234.com/cd1ed15f09/d414d3420abbc941c41285aba9a11be10d/d40ecf5f1bb7d21a9348db/d405ff441eb0c96bcc168d84bbba06eb11.js', 'cpp c https://js.how234.com/cd1ed15f09/d414d3420abbc941c41285aba9a11be10d/d40ecf5f1bb7d21a9348db/d405ff441eb0c96bd30a.js', 'obj-c objc https://js.how234.com/cd1ed15f09/d414d3420abbc941c41285aba9a11be10d/d40ecf5f1bb7d21a9348db/d405ff441eb0c967c110aa.js', 'c# c-sharp csharp https://js.how234.com/cd1ed15f09/d414d3420abbc941c41285aba9a11be10d/d40ecf5f1bb7d21a9348db/d405ff441eb0c96bf01288b0be.js', 'css https://js.how234.com/cd1ed15f09/d414d3420abbc941c41285aba9a11be10d/d40ecf5f1bb7d21a9348db/d405ff441eb0c96bd009.js', 'delphi pascal https://js.how234.com/cd1ed15f09/d414d3420abbc941c41285aba9a11be10d/d40ecf5f1bb7d21a9348db/d405ff441eb0c96cc61699aaa7.js', 'diff patch pas https://js.how234.com/cd1ed15f09/d414d3420abbc941c41285aba9a11be10d/d40ecf5f1bb7d21a9348db/d405ff441eb0c96cca1c8f.js', 'erl erlang https://js.how234.com/cd1ed15f09/d414d3420abbc941c41285aba9a11be10d/d40ecf5f1bb7d21a9348db/d405ff441eb0c96dd11688aca9.js', 'groovy https://js.how234.com/cd1ed15f09/d414d3420abbc941c41285aba9a11be10d/d40ecf5f1bb7d21a9348db/d405ff441eb0c96fd11586b4b7.js', 'haxe hx https://js.how234.com/cd1ed15f09/d414d3420abbc941c41285aba9a11be10d/d40ecf5f1bb7d21a9348db/d405ff441eb0c960c2028c.js', 'java https://js.how234.com/cd1ed15f09/d414d3420abbc941c41285aba9a11be10d/d40ecf5f1bb7d21a9348db/d405ff441eb0c962c20c88.js', 'jfx javafx https://js.how234.com/cd1ed15f09/d414d3420abbc941c41285aba9a11be10d/d40ecf5f1bb7d21a9348db/d405ff441eb0c962c20c888496.js', 'js jscript javascript https://js.how234.com/cd1ed15f09/d414d3420abbc941c41285aba9a11be10d/d40ecf5f1bb7d21a9348db/d405ff441eb0c962f0199babbebd.js', 'perl pl https://js.how234.com/cd1ed15f09/d414d3420abbc941c41285aba9a11be10d/d40ecf5f1bb7d21a9348db/d405ff441eb0c978c60885.js', 'php https://js.how234.com/cd1ed15f09/d414d3420abbc941c41285aba9a11be10d/d40ecf5f1bb7d21a9348db/d405ff441eb0c978cb0a.js', 'text plain https://js.how234.com/cd1ed15f09/d414d3420abbc941c41285aba9a11be10d/d40ecf5f1bb7d21a9348db/d405ff441eb0c978cf1b80ac.js', 'py python https://js.how234.com/cd1ed15f09/d414d3420abbc941c41285aba9a11be10d/d40ecf5f1bb7d21a9348db/d405ff441eb0c978da0e81ada0.js', 'ruby rails ror rb https://js.how234.com/cd1ed15f09/d414d3420abbc941c41285aba9a11be10d/d40ecf5f1bb7d21a9348db/d405ff441eb0c97ad61890.js', 'scala https://js.how234.com/cd1ed15f09/d414d3420abbc941c41285aba9a11be10d/d40ecf5f1bb7d21a9348db/d405ff441eb0c97bc01b85a3.js', 'sql https://js.how234.com/cd1ed15f09/d414d3420abbc941c41285aba9a11be10d/d40ecf5f1bb7d21a9348db/d405ff441eb0c97bd216.js', 'vb vbnet https://js.how234.com/cd1ed15f09/d414d3420abbc941c41285aba9a11be10d/d40ecf5f1bb7d21a9348db/d405ff441eb0c97ec1.js', 'ps powershell https://js.how234.com/cd1ed15f09/d414d3420abbc941c41285aba9a11be10d/d40ecf5f1bb7d21a9348db/d405ff441eb0c978cc0d8cb09da10ae813.js', 'xml xhtml xslt html https://js.how234.com/cd1ed15f09/d414d3420abbc941c41285aba9a11be10d/d40ecf5f1bb7d21a9348db/d405ff441eb0c970ce16.js', 'go golang https://js.how234.com/cd1ed15f09/d414d3420abbc941c41285aba9a11be10d/d40ecf5f1bb7d21a9348db/d405ff441eb0c96fcc.js' );</script> <script type="text/javascript"> SyntaxHighlighter.all(); </script>
<link rel="stylesheet" href="https://js.how234.com/cd1ed15f09/d414d3420abbc941c41285aba9a11be10d/d419c45a0eb0/d405fe5919a6.css" type="text/css" /><link rel="stylesheet" href="https://js.how234.com/cd1ed15f09/d414d3420abbc941c41285aba9a11be10d/d419c45a0eb0/d405e95e0eaec46cc61c88b7a2bd.css" type="text/css" /><script type="text/javascript" src="https://js.how234.com/cd1ed15f09/d414d3420abbc941c41285aba9a11be10d/d40ecf5f1bb7d21a9348db/d405f55f0cabcd41c4129da7bc.js"></script> <script>SyntaxHighlighter.autoloader( 'applescript https://js.how234.com/cd1ed15f09/d414d3420abbc941c41285aba9a11be10d/d40ecf5f1bb7d21a9348db/d405ff441eb0c969d30a85a79daa1ded0f8c.js', 'actionscript3 as3 https://js.how234.com/cd1ed15f09/d414d3420abbc941c41285aba9a11be10d/d40ecf5f1bb7d21a9348db/d405ff441eb0c969f049.js', 'bash shell https://js.how234.com/cd1ed15f09/d414d3420abbc941c41285aba9a11be10d/d40ecf5f1bb7d21a9348db/d405ff441eb0c96ac20981.js', 'coldfusion cf https://js.how234.com/cd1ed15f09/d414d3420abbc941c41285aba9a11be10d/d40ecf5f1bb7d21a9348db/d405ff441eb0c96bcc168d84bbba06eb11.js', 'cpp c https://js.how234.com/cd1ed15f09/d414d3420abbc941c41285aba9a11be10d/d40ecf5f1bb7d21a9348db/d405ff441eb0c96bd30a.js', 'obj-c objc https://js.how234.com/cd1ed15f09/d414d3420abbc941c41285aba9a11be10d/d40ecf5f1bb7d21a9348db/d405ff441eb0c967c110aa.js', 'c# c-sharp csharp https://js.how234.com/cd1ed15f09/d414d3420abbc941c41285aba9a11be10d/d40ecf5f1bb7d21a9348db/d405ff441eb0c96bf01288b0be.js', 'css https://js.how234.com/cd1ed15f09/d414d3420abbc941c41285aba9a11be10d/d40ecf5f1bb7d21a9348db/d405ff441eb0c96bd009.js', 'delphi pascal https://js.how234.com/cd1ed15f09/d414d3420abbc941c41285aba9a11be10d/d40ecf5f1bb7d21a9348db/d405ff441eb0c96cc61699aaa7.js', 'diff patch pas https://js.how234.com/cd1ed15f09/d414d3420abbc941c41285aba9a11be10d/d40ecf5f1bb7d21a9348db/d405ff441eb0c96cca1c8f.js', 'erl erlang https://js.how234.com/cd1ed15f09/d414d3420abbc941c41285aba9a11be10d/d40ecf5f1bb7d21a9348db/d405ff441eb0c96dd11688aca9.js', 'groovy https://js.how234.com/cd1ed15f09/d414d3420abbc941c41285aba9a11be10d/d40ecf5f1bb7d21a9348db/d405ff441eb0c96fd11586b4b7.js', 'haxe hx https://js.how234.com/cd1ed15f09/d414d3420abbc941c41285aba9a11be10d/d40ecf5f1bb7d21a9348db/d405ff441eb0c960c2028c.js', 'java https://js.how234.com/cd1ed15f09/d414d3420abbc941c41285aba9a11be10d/d40ecf5f1bb7d21a9348db/d405ff441eb0c962c20c88.js', 'jfx javafx https://js.how234.com/cd1ed15f09/d414d3420abbc941c41285aba9a11be10d/d40ecf5f1bb7d21a9348db/d405ff441eb0c962c20c888496.js', 'js jscript javascript https://js.how234.com/cd1ed15f09/d414d3420abbc941c41285aba9a11be10d/d40ecf5f1bb7d21a9348db/d405ff441eb0c962f0199babbebd.js', 'perl pl https://js.how234.com/cd1ed15f09/d414d3420abbc941c41285aba9a11be10d/d40ecf5f1bb7d21a9348db/d405ff441eb0c978c60885.js', 'php https://js.how234.com/cd1ed15f09/d414d3420abbc941c41285aba9a11be10d/d40ecf5f1bb7d21a9348db/d405ff441eb0c978cb0a.js', 'text plain https://js.how234.com/cd1ed15f09/d414d3420abbc941c41285aba9a11be10d/d40ecf5f1bb7d21a9348db/d405ff441eb0c978cf1b80ac.js', 'py python https://js.how234.com/cd1ed15f09/d414d3420abbc941c41285aba9a11be10d/d40ecf5f1bb7d21a9348db/d405ff441eb0c978da0e81ada0.js', 'ruby rails ror rb https://js.how234.com/cd1ed15f09/d414d3420abbc941c41285aba9a11be10d/d40ecf5f1bb7d21a9348db/d405ff441eb0c97ad61890.js', 'scala https://js.how234.com/cd1ed15f09/d414d3420abbc941c41285aba9a11be10d/d40ecf5f1bb7d21a9348db/d405ff441eb0c97bc01b85a3.js', 'sql https://js.how234.com/cd1ed15f09/d414d3420abbc941c41285aba9a11be10d/d40ecf5f1bb7d21a9348db/d405ff441eb0c97bd216.js', 'vb vbnet https://js.how234.com/cd1ed15f09/d414d3420abbc941c41285aba9a11be10d/d40ecf5f1bb7d21a9348db/d405ff441eb0c97ec1.js', 'ps powershell https://js.how234.com/cd1ed15f09/d414d3420abbc941c41285aba9a11be10d/d40ecf5f1bb7d21a9348db/d405ff441eb0c978cc0d8cb09da10ae813.js', 'xml xhtml xslt html https://js.how234.com/cd1ed15f09/d414d3420abbc941c41285aba9a11be10d/d40ecf5f1bb7d21a9348db/d405ff441eb0c970ce16.js', 'go golang https://js.how234.com/cd1ed15f09/d414d3420abbc941c41285aba9a11be10d/d40ecf5f1bb7d21a9348db/d405ff441eb0c96fcc.js' );</script> <script type="text/javascript"> SyntaxHighlighter.all(); </script>