How to generate unique numeric id in php
Database systems use exclusive locking when creating numbers such as MySQL's Show You have to approach the problem you have the same way - acquire a lock from the PHP process that's serving the request, look up the current value within some sort of persistent storage, increment it by 1, return it and release the lock. The easiest way to do this is to use a good old file and exclusive locking. I'll illustrate with a class (which should be debugged since it's not complete):
Usage: (PHP 4, PHP 5, PHP 7, PHP 8) uniqid — Generate a unique ID Descriptionuniqid(string Warning This function does not
guarantee uniqueness of return value. Since most systems adjust system clock by NTP or like, system time is changed constantly. Therefore, it is possible that this function does not return unique ID for the process/thread. Use Parametersprefix Can be useful, for instance, if you generate identifiers simultaneously on several hosts that might happen to generate the identifier at the same microsecond. With an empty more_entropy If set to Return ValuesReturns timestamp based unique identifier as a string. Warning This function tries to create unique identifier, but it does not guarantee 100% uniqueness of return value. ExamplesExample #1 uniqid() Example
Notes
keith at keithtyler dot com ¶ 12 years ago
Andrew Moore ¶ 12 years ago
sprintf('%08s-%04s-%04x-%04x-%12s',// 32 bits for "time_low" hackan at gmail dot com ¶ 5 years ago
John Haugeland from FullOf.BS ¶ 13 years ago
rommel at rommelsantor dot com ¶ 11 years ago
strlen($i_url)) nodkz at mail dot ru ¶ 14 years ago
=uuid(); // 0001-7f000001-478c8000-4801-47242987 DimeCadmium ¶ 3 years ago
redtraider at gmail dot com ¶ 10 years ago
Enrico Pallazzo ¶ 12 years ago
$n=0; $n < strlen($index); $n++) { Anonymous ¶ 11 years ago
wooshoofoo ¶ 15 years ago
php at dot dot dot metehanarslan dot com ¶ 6 years ago
struuid(false); //Return sample: F4518NTQTQ mailrinke at _cutthis_yahoo dot com ¶ 15 years ago
ken at smallboxsoftware ¶ 15 years ago
david at ramaboo dot com ¶ 13 years ago
sprintf('%08s-%04s-%04x-%04x-%012s', php at ryanmckeel dot com ¶ 12 years ago
ciantic at NOSPAM dot oksidi dot com ¶ 8 years ago
uniqid_base36() . "\n"; // eb98xzzhq7 mt at banita dot de ¶ 1 year ago
sean at seancolombo dot com ¶ 13 years ago
sprintf('%08s-%04s-%04x-%04x-%012s', tom ¶ 11 years ago
jensenv at cshs dot org ¶ 7 years ago
Anonymous ¶ 9 years ago
lance_rushing at hotmail dot com ¶ 15 years ago
mark at whytewaters dot com ¶ 14 years ago
Jason ¶ 15 years ago
How to create a unique ID in PHP?A unique user ID can be created in PHP using the uniqid () function. This function has two parameters you can set. The first is the prefix, which is what will be appended to the beginning of each ID. The second is more_entropy.
How to get unique number in PHP?The uniqid() function in PHP is an inbuilt function which is used to generate a unique ID based on the current time in microseconds (micro time). The ID generated from the uniqid() function is not optimal since it is based on the system time and is not cryptographically secured.
Which PHP function is used to generate unique ID?The uniqid() function generates a unique ID based on the microtime (the current time in microseconds). Note: The generated ID from this function does not guarantee uniqueness of the return value! To generate an extremely difficult to predict ID, use the md5() function.
|