Generating a unique Key
I need a unique Key to use to identify an object.
I use this in PHP:
$key = md5(uniqid(mt_rand(), false));
This should be unique, but can you believe that will never be repeated? I don’t trust it…
Just check it in your DB just in case:
function generateKey(){//Generate a unique key do{ $key = md5(uniqid(mt_rand(), false)); } while(validateKeyn($key)); return $key; } function validateKey($key){//check the DB $query = "SELECT theKey FROM Table WHERE theKey ='".trim($key)."' LIMIT 1"; $result=mysql_query($query); if (mysql_num_rows($result)) return true; else return false; }
Also imagine you need to create a kind of serial number, you can use this interesting function from corner:
// Generate Guid function NewGuid() { $s = strtoupper(md5(uniqid(rand(),true))); $guidText = substr($s,0,8) . '-' . substr($s,8,4) . '-' . substr($s,12,4). '-' . substr($s,16,4). '-' . substr($s,20); return $guidText; } // End Generate Guid $Guid = NewGuid();
This will return something like: XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX style unique id, (8 letters)-(4 letters)-(4 letters)-(4 letters)-(12 letters)
Update: I just found that is “a unique” instead of “an unique” xD
Related Posts- PHP sitemap.xml generator
- Calculate movie hash
- phpSEO - Class for better SEO in PHP
- Prevent code injection in PHP
- Add PDF files inside other PDF in PHP
- Google Fusion Tables Could be a Game Changer
- Review: The Teen's Guide to Personal Finance: Basic concepts in personal finance that every teen should know.
- 5 Elements for Successfully Writing a Blog
- Key To SEO? Get Someone To Generate Content For You
- Back and other hard keys: three stories
Help sharing and Flatter me ;)
