× {{alert.msg}} Never ask again
Get notified about new tutorials RECEIVE NEW TUTORIALS

[Code Snippet] PHP - Generate Unique User IDs

Rob Heller
Dec 31, 2015
<p>Anytime I have a database with IDs I use this function I found long ago to generate a unique 36 character ID. </p> <p>You can use and call the function like this:</p> <pre><code class="language-php">echo generateUUID();</code></pre> <p>this is return a ID like this:</p> <pre><code class="language-php">8931CB45-0DF2-905E-CB7D-7426BE13CD61</code></pre> <p>which calls this:</p> <pre><code class="language-php">function generateUUID(){ 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 = 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; } }</code></pre> <p> </p> <p>Enjoy</p>
comments powered by Disqus