Re: [PHP] Re: Random Unique ID
On Wed, March 21, 2007 5:40 pm, [EMAIL PROTECTED] wrote: > Thanks. Yes, I check for errors. But there are other types of errors > so I'd need to verify that it is a duplicate key error and, in my > ignorance, I have not yet figured out how to do that programatically. > I worry about getting into an infinite loop. Untested code: //create table test_id (id char(32) unique not null primary key)); $success = 0; while (!$success){ $id = md5(uniqid()); $query = "insert into test_id (id) values('$id')"; $result = mysql_query($query); if (!$result){ //I don't promise 1023 is right. //force a duplicate as a test to confirm if (mysql_errno() == 1023){ //duplicate key //we'll loop back and try with another } else{ echo "Something went wrong with our database. Sorry."; error_log(mysql_error()); break; } } else{ $success = 1; } } -- Some people have a "gift" link here. Know what I want? I want you to buy a CD from some indie artist. http://cdbaby.com/browse/from/lynch Yeah, I get a buck. So? -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Re: Random Unique ID
I'm sorry if I am repeating something that has been said, but in the comments to: http://es2.php.net/manual/en/function.uniqid.php there is a little piece of code that generates UUID as per RFC 4122, and those get as unique as they come. MySql has a UUID() function and so do any database engine that supports replication, since that's the only way to ensure keys won't get repeated over multiple servers. Satyam - Original Message - From: <[EMAIL PROTECTED]> To: Sent: Friday, March 23, 2007 7:48 PM Subject: [PHP] Re: Random Unique ID Robert Cummings writes: Bleh, you can solve this with at most 2 queries. Thanks. That is a bit different than what I was thinking about but it might work for my purposes. Thanks also to all the other people who made suggestions. It appears to me that ther are 3 distinct approaches. 1) Generate a random number. Query the DB to see if it has been used before and if so generate another random number and repeat. It seems to me that this would require that the DB be locked for the checking and insertion to make sure a duplicate was not slipped in. 2) Generate a random number. Make a trial insertion for a unique field. If the insertion fails repeat. This functions just like the above but uses Mysql's native behavior to handle the checking so no user locking is necessary. Almost all the time this will require only 1 insertion whereas the above will require 2 or more accesses. 3) Generate a unique identifier from the record sequence number. Since the sequence number is guaranteed to be unique the identifier will also be unique and can be used without any worry. There are various schemes I can think of by which the unique identifier, altho uniquely determined by the sequence number, can be generated so that it will not reveal the sequence number and appear unpredictable. This will require 3 accesses to the DB (insertion, mysql_insert_id, and update). I have a number of options to try and think about. Thanks again. Best, Craig -- - Virtual Phonecards - Instant Pin by Email - - Large Selection - Great Rates- - http://speedypin.com/?aff=743&co_branded=1 - -- ** ** * Craig Spencer * * [EMAIL PROTECTED]* ** ** -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php -- No virus found in this incoming message. Checked by AVG Free Edition. Version: 7.5.446 / Virus Database: 268.18.17/730 - Release Date: 22/03/2007 7:44 -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Re: Random Unique ID
Tijnema ! wrote: > On 3/22/07, markw@mohawksoft.com wrote: >> > On 3/22/07, Roman Neuhauser <[EMAIL PROTECTED]> wrote: >> >> # markw@mohawksoft.com / 2007-03-22 08:49:59 -0400: >> >> > Tijnema ! wrote: >> >> > >> >> > > On 3/22/07, Roman Neuhauser <[EMAIL PROTECTED]> wrote: >> >> > >> >> >> > >> http://www.ossp.org/pkg/lib/uuid >> >> > > >> >> > > Note that this doesn't gonna work when safe mode is on..! >> >> >> >> Ralf S. Engelschall's OSSP UUID library wrapped in a PHP extension >> >> (shipped with the library) won't work in safe mode? How's that? >> > >> > I quote the wrong guy i think, i meant to quote this >> >> If you are running Linux, you can get this using: >> >>$GUID = exec("uuidgen"); >> > As this is using the exec function, which doesn't work with safe mode >> > on. >> > >> > >> >> >> >> > Generating a psudo-random unique key doesn't seem to be something >> >> > PHP >> >> does >> >> > well. I can post an extension this afternoon if anyone wants to use >> >> it. >> >> >> >> I'm curious. >> >> >> >> Take a hop over to www.mohawksoft.org, and check out "download," download >> MUniqID and tell me what you think. >> >> It should build as a module. >> >> > I'm not sure where you want to use it for, but keep in mind that if > you are gonna use external modules/applications it will probably work > only on your development server. Not everyone (nearly nobody) has such > modules installed, so if you are planning to distribute, you need to > keep in mind that everyone that want's to use the script, will need to > install the used module/app. Well, it can build as part of PHP if you do a buildconf as well -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Re: Random Unique ID
On 3/22/07, markw@mohawksoft.com wrote: > On 3/22/07, Roman Neuhauser <[EMAIL PROTECTED]> wrote: >> # markw@mohawksoft.com / 2007-03-22 08:49:59 -0400: >> > Tijnema ! wrote: >> > >> > > On 3/22/07, Roman Neuhauser <[EMAIL PROTECTED]> wrote: >> > >> >> > >> http://www.ossp.org/pkg/lib/uuid >> > > >> > > Note that this doesn't gonna work when safe mode is on..! >> >> Ralf S. Engelschall's OSSP UUID library wrapped in a PHP extension >> (shipped with the library) won't work in safe mode? How's that? > > I quote the wrong guy i think, i meant to quote this >> If you are running Linux, you can get this using: >>$GUID = exec("uuidgen"); > As this is using the exec function, which doesn't work with safe mode on. > > >> >> > Generating a psudo-random unique key doesn't seem to be something PHP >> does >> > well. I can post an extension this afternoon if anyone wants to use >> it. >> >> I'm curious. >> Take a hop over to www.mohawksoft.org, and check out "download," download MUniqID and tell me what you think. It should build as a module. I'm not sure where you want to use it for, but keep in mind that if you are gonna use external modules/applications it will probably work only on your development server. Not everyone (nearly nobody) has such modules installed, so if you are planning to distribute, you need to keep in mind that everyone that want's to use the script, will need to install the used module/app. Tijnema -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Re: Random Unique ID
> On 3/22/07, Roman Neuhauser <[EMAIL PROTECTED]> wrote: >> # markw@mohawksoft.com / 2007-03-22 08:49:59 -0400: >> > Tijnema ! wrote: >> > >> > > On 3/22/07, Roman Neuhauser <[EMAIL PROTECTED]> wrote: >> > >> >> > >> http://www.ossp.org/pkg/lib/uuid >> > > >> > > Note that this doesn't gonna work when safe mode is on..! >> >> Ralf S. Engelschall's OSSP UUID library wrapped in a PHP extension >> (shipped with the library) won't work in safe mode? How's that? > > I quote the wrong guy i think, i meant to quote this >> If you are running Linux, you can get this using: >>$GUID = exec("uuidgen"); > As this is using the exec function, which doesn't work with safe mode on. > > >> >> > Generating a psudo-random unique key doesn't seem to be something PHP >> does >> > well. I can post an extension this afternoon if anyone wants to use >> it. >> >> I'm curious. >> Take a hop over to www.mohawksoft.org, and check out "download," download MUniqID and tell me what you think. It should build as a module. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Re: Random Unique ID
On 3/22/07, Roman Neuhauser <[EMAIL PROTECTED]> wrote: # markw@mohawksoft.com / 2007-03-22 08:49:59 -0400: > Tijnema ! wrote: > > > On 3/22/07, Roman Neuhauser <[EMAIL PROTECTED]> wrote: > >> > >> http://www.ossp.org/pkg/lib/uuid > > > > Note that this doesn't gonna work when safe mode is on..! Ralf S. Engelschall's OSSP UUID library wrapped in a PHP extension (shipped with the library) won't work in safe mode? How's that? I quote the wrong guy i think, i meant to quote this If you are running Linux, you can get this using: $GUID = exec("uuidgen"); As this is using the exec function, which doesn't work with safe mode on. > Generating a psudo-random unique key doesn't seem to be something PHP does > well. I can post an extension this afternoon if anyone wants to use it. I'm curious. -- How many Vietnam vets does it take to screw in a light bulb? You don't know, man. You don't KNOW. Cause you weren't THERE. http://bash.org/?255991 -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Re: Random Unique ID
# markw@mohawksoft.com / 2007-03-22 08:49:59 -0400: > Tijnema ! wrote: > > > On 3/22/07, Roman Neuhauser <[EMAIL PROTECTED]> wrote: > >> > >> http://www.ossp.org/pkg/lib/uuid > > > > Note that this doesn't gonna work when safe mode is on..! Ralf S. Engelschall's OSSP UUID library wrapped in a PHP extension (shipped with the library) won't work in safe mode? How's that? > Generating a psudo-random unique key doesn't seem to be something PHP does > well. I can post an extension this afternoon if anyone wants to use it. I'm curious. -- How many Vietnam vets does it take to screw in a light bulb? You don't know, man. You don't KNOW. Cause you weren't THERE. http://bash.org/?255991 -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Re: Random Unique ID
Tijnema ! wrote: > On 3/22/07, Roman Neuhauser <[EMAIL PROTECTED]> wrote: >> # [EMAIL PROTECTED] / 2007-03-21 21:03:35 -0500: >> > Mark wrote: >> > >[EMAIL PROTECTED] 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 suggest looking into a GUID sort of thing, it is all coded for you >> > >and it works just like you want. >> > >> > If you are running Linux, you can get this using: >> >$GUID = exec("uuidgen"); >> >> http://www.ossp.org/pkg/lib/uuid > > Note that this doesn't gonna work when safe mode is on..! Generating a psudo-random unique key doesn't seem to be something PHP does well. I can post an extension this afternoon if anyone wants to use it. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Re: Random Unique ID
On 3/22/07, Roman Neuhauser <[EMAIL PROTECTED]> wrote: # [EMAIL PROTECTED] / 2007-03-21 21:03:35 -0500: > Mark wrote: > >[EMAIL PROTECTED] 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 suggest looking into a GUID sort of thing, it is all coded for you and it > >works just like you want. > > If you are running Linux, you can get this using: >$GUID = exec("uuidgen"); http://www.ossp.org/pkg/lib/uuid Note that this doesn't gonna work when safe mode is on..! -- How many Vietnam vets does it take to screw in a light bulb? You don't know, man. You don't KNOW. Cause you weren't THERE. http://bash.org/?255991 -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Re: Random Unique ID
# [EMAIL PROTECTED] / 2007-03-21 21:03:35 -0500: > Mark wrote: > >[EMAIL PROTECTED] 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 suggest looking into a GUID sort of thing, it is all coded for you and it > >works just like you want. > > If you are running Linux, you can get this using: >$GUID = exec("uuidgen"); http://www.ossp.org/pkg/lib/uuid -- How many Vietnam vets does it take to screw in a light bulb? You don't know, man. You don't KNOW. Cause you weren't THERE. http://bash.org/?255991 -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Re: Random Unique ID
On Thu, 2007-03-22 at 08:14 +0400, Rabih Tayyem wrote: > Actually I told you that the possibility of collision is small because I > wanted to be scientific > Scientifically, I can say this posibilty goes to 0... Only as the length of the ID approaches infinity :B Cheers, Rob. -- .. | InterJinn Application Framework - http://www.interjinn.com | :: | An application and templating framework for PHP. Boasting | | a powerful, scalable system for accessing system services | | such as forms, properties, sessions, and caches. InterJinn | | also provides an extremely flexible architecture for | | creating re-usable components quickly and easily. | `' -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Re: Random Unique ID
Actually I told you that the possibility of collision is small because I wanted to be scientific Scientifically, I can say this posibilty goes to 0... It is a random ID (not a totally random but based on the current timestamp) generated not only once but twice based on two different time stamps. Safe enough I believe... Regards, Rabih G. Tayyem On 3/22/07, Jim Moseby <[EMAIL PROTECTED]> wrote: > Rabih Tayyem writes: > > > PS: I don't take credit for the code as it is a modified version of > > a code I found long time back (this same code is running on one of > > my applications for months without any problem).. > > Thanks. I'll find use for that! > > However, altho I know that by making the random number big enough > the likelyhood of collisions can be made vanishingly small, I was > actually concerned with eliminating the possibility of collisions > altogether by checking to see if the number had been used before. > > I just don't know how to do that properly with Mysql. Perhaps it > is necessary to lock to table, check, make the insert and then > unlock it. But I was hoping that there would be a simpler way. Replied off-list with a solution. JM -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Re: Random Unique ID
On Wed, 2007-03-21 at 16:40 -0600, [EMAIL PROTECTED] wrote: > Jim Moseby writes: > > >> However, altho I know that by making the random number big enough > >> the likelyhood of collisions can be made vanishingly small, I was > >> actually concerned with eliminating the possibility of collisions > >> altogether by checking to see if the number had been used before. > >> > >> I just don't know how to do that properly with Mysql. Perhaps it > >> is necessary to lock to table, check, make the insert and then > >> unlock it. But I was hoping that there would be a simpler way. > > > > One way is to make your id field a unique key. MySQL will not let > > you insert a record with a duplicate unique key, and will issue an > > error. Your code should always check for errors on insert anyway, > > so if you get an error, generate a new key and try again. > > Thanks. Yes, I check for errors. But there are other types of errors > so I'd need to verify that it is a duplicate key error and, in my > ignorance, I have not yet figured out how to do that programatically. > I worry about getting into an infinite loop. Bleh, you can solve this with at most 2 queries. CREATE TABLE foo ( id INT NOT NULL AUTO_INCREMENT, uid char( 50 ), PRIMARY KEY ( id ) ); There you go, a 50 character highly unguessable and guaranteed unique ID. Sure everyone knows the ID of the entry now, but that shouldn't be important. In fact it optimizes retrieval and validation by allowing the lookup to occur on an integer then validation by matching the full UID against the UID found for the ID. In all honesty though, hitting the database with random generated md5() hashes is probably more efficient since the likelihood of a collision is small and so in most cases you will only make one database query. Cheers, Rob. -- .. | InterJinn Application Framework - http://www.interjinn.com | :: | An application and templating framework for PHP. Boasting | | a powerful, scalable system for accessing system services | | such as forms, properties, sessions, and caches. InterJinn | | also provides an extremely flexible architecture for | | creating re-usable components quickly and easily. | `' -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Re: Random Unique ID
[EMAIL PROTECTED] wrote: Stut writes: [EMAIL PROTECTED] wrote: 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? 1) Not even slightly PHP related. Perhaps not. It is Mysql related. And seeing other Mysql discussion on this list inspired me to ask. I refer you to the comment made earlier today by one of my honourable colleagues regarding murder. 2) Why random? Incrementing not good enough for you? I want to use it in a way that will expose it to the user. And I don't want to give out information as to the order of records in my database. This is not security. Your site should prevent them from accessing data they should not be accessing. If you're worried that they can do this simply by changing a number in a query string then you really need to rethink the way the site works. If you decide to ignore my advice, then the only way to do this is to set the ID field as unique, pick a random number and try to insert the row. Repeat until the insert succeeds. Yes it sucks, but so does what you are trying to do. -Stut -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
RE: [PHP] Re: Random Unique ID
> Rabih Tayyem writes: > > > PS: I don't take credit for the code as it is a modified version of > > a code I found long time back (this same code is running on one of > > my applications for months without any problem).. > > Thanks. I'll find use for that! > > However, altho I know that by making the random number big enough > the likelyhood of collisions can be made vanishingly small, I was > actually concerned with eliminating the possibility of collisions > altogether by checking to see if the number had been used before. > > I just don't know how to do that properly with Mysql. Perhaps it > is necessary to lock to table, check, make the insert and then > unlock it. But I was hoping that there would be a simpler way. Replied off-list with a solution. JM -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php