{"id":2298,"date":"2004-10-29T14:31:44","date_gmt":"2004-10-29T14:31:44","guid":{"rendered":"http:\/\/www.soulhuntre.com\/items\/date\/2004\/10\/29\/uuid-guid-in-native-php\/"},"modified":"2004-10-29T14:31:44","modified_gmt":"2004-10-29T14:31:44","slug":"uuid-guid-in-native-php","status":"publish","type":"post","link":"http:\/\/legacyiamsenseiken.local\/2004\/10\/29\/uuid-guid-in-native-php\/","title":{"rendered":"UUID \/ GUID in native PHP…"},"content":{"rendered":"
In the course of my development efforts it has become fairly clear that I need to make use of a more cross platform and cross server identifier for certain content objects. The common tactic of using a database column that automatically increments has fallen pretty flat for this – it is almost guaranteed to collide if you try and merge tables or data in the future. The code is below the fold!<\/b><\/p>\n
Microsoft databases have the concept of a [[wp;GUID]] that they implement in a nice simple manner<\/a> and that is supported well in .NET<\/a>.Nothing similar exists in MySQL<\/a> and PHP<\/a> at the moment, though some tantalizing hints existed. Long and short of it, I solved the problem, or at least I have an early attempt at solving the problem. Compare it to the numbers you can get at this online UUID generator<\/a>.<\/p>\n <\/p>\n This code is a combination of…<\/p>\n So enjoy it, and please [[let me know]] if I missed anything! BOh, and a CHAR(32) in MySQL is a good place to store these.<\/p>\n \/\/ -_-_–_-_–_-_–_-_—<\/p>\n \/\/ build a UUID or GUID via PHP $rawid = strtoupper(md5(uniqid(rand(), true))); \/\/ hopefully conform to the spec, mark this as a “random” type \/\/ hopefully conform to the spec, mark this common variant \/\/ build a human readable version \/\/ build a human readable version \/\/ -_-_–_-_–_-_–_-_—<\/p>\n<\/blockquote>\n An example of the output….<\/p>\n A2C2817A-F2CE-49B1-BF4C-DB0961998E2A In the course of my development efforts it has become fairly clear that I need to make use of a more cross platform and cross server identifier for certain content objects. The common tactic of using a database column that automatically increments has fallen pretty flat for this – it is almost guaranteed to collide […]<\/p>\n","protected":false},"author":3,"featured_media":53202,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"aside","meta":{"footnotes":""},"categories":[278],"tags":[],"_links":{"self":[{"href":"http:\/\/legacyiamsenseiken.local\/wp-json\/wp\/v2\/posts\/2298"}],"collection":[{"href":"http:\/\/legacyiamsenseiken.local\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/legacyiamsenseiken.local\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/legacyiamsenseiken.local\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"http:\/\/legacyiamsenseiken.local\/wp-json\/wp\/v2\/comments?post=2298"}],"version-history":[{"count":0,"href":"http:\/\/legacyiamsenseiken.local\/wp-json\/wp\/v2\/posts\/2298\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"http:\/\/legacyiamsenseiken.local\/wp-json\/wp\/v2\/media\/53202"}],"wp:attachment":[{"href":"http:\/\/legacyiamsenseiken.local\/wp-json\/wp\/v2\/media?parent=2298"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/legacyiamsenseiken.local\/wp-json\/wp\/v2\/categories?post=2298"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/legacyiamsenseiken.local\/wp-json\/wp\/v2\/tags?post=2298"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}\n
\n
\/\/ may or may not be Microsoft GUID compatible
\/\/ thanks to all the internet code examples!
\/\/
\/\/ contact me with corrections and changes please,
\/\/ soulhuntre@soulhuntre.com
\/\/
\/\/ 10\/29\/2004 – v1.0
\/\/
\/\/ Do whatever you want with this code, it’s in the public domain<\/p>\n
$workid = $rawid;<\/p>\n
\/\/ lets handle the version byte as a number
$byte = hexdec( substr($workid,12,2) );
$byte = $byte & hexdec(“0f”);
$byte = $byte | hexdec(“40”);
$workid = substr_replace($workid, strtoupper(dechex($byte)), 12, 2);<\/p>\n
\/\/ lets handle the “variant”
$byte = hexdec( substr($workid,16,2) );
$byte = $byte & hexdec(“3f”);
$byte = $byte | hexdec(“80”);
$workid = substr_replace($workid, strtoupper(dechex($byte)), 16, 2);<\/p>\n
$rid = substr($rawid, 0, 8).’-‘
.substr($rawid, 8, 4).’-‘
.substr($rawid,12, 4).’-‘
.substr($rawid,16, 4).’-‘
.substr($rawid,20,12);<\/p>\n
$wid = substr($workid, 0, 8).’-‘
.substr($workid, 8, 4).’-‘
.substr($workid,12, 4).’-‘
.substr($workid,16, 4).’-‘
.substr($workid,20,12);<\/p>\n\n
9BCF2CD8-A5E5-4331-BD71-491804CE6ED6
575D3B15-19ED-4A50-A773-0FFD3631F391
AF457D46-7BF6-4F7F-B718-47EAD9FCD47E
8138B63D-0CC1-4E35-8641-A67FF0D3486D
6F4AD196-45FD-4B4D-88F7-68F9C009365C
930ED1AB-4335-4DF2-AB05-DFFC3E349295
B6097E8A-3CAD-492B-9DB9-59697AE97BA6
7944D65B-82DB-4946-81F5-D6527227D422
15BC5F60-B731-4C0B-9F88-8C71F1196FE1<\/p>\n<\/blockquote>\n","protected":false},"excerpt":{"rendered":"