Re: Random Unique ID

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



ccspencer@xxxxxxxxxxxx wrote:

> Hello,
> 
> I want to add a random unique ID to a Mysql table.  Collisions
> are unlikely but possible so to handle those cases I'd like to
> regenerate the random ID until there is no collision and only
> then add my row.  Any suggestions for a newbie as to the right
> way to go about doing this?
> 
> Best,
> 
> Craig
> 
I just wrote up a quick PHP extension that will do what you want. It is on
my website, it is called muniqid, you can get it from the download area.
You can build it as part of PHP or used phpize, configure, make, make
install and create a module.

get it at http://ww.mohawksoft.org

It provides two functions:

muniqid_time(), and muniqid_rand().

muniqid_time() creates a GUID looking string that incorporates, time in
seconds, timer ticks, pid, ip address, and system stats to create an almost
certainly unique key with enough random data to make it impossible to
guess.

A duplicate is only possible within your web site (I guess it could happen
somewhere in the world), if you are using SMP motherboards and both
processors are processing a create ID, at *exactly* the same time (within
resolution of clock ticks) and are running in two threads within the same
process and rand() is isolated between threads and both threads have
processed the same number of IDs. Short of that, pretty impossible.

muniqid_rand() creates a GUID looking string that incorporates a lot of
random data from your machine. It may be less unique than muniqid_time, but
is far more random.

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux