Bluehost.com Web Hosting $6.95

unique id

This is a discussion on unique id within the PHP General forums, part of the PHP Programming Forums category; Hi there! What's the best way to create uids (unique ids) even when runnig at exactly same time (microseconds)? ...


Go Back   Usenet Forums > PHP Programming Forums > PHP General

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 07-26-2003
Jan
 
Posts: n/a
Default unique id

Hi there!

What's the best way to create uids (unique ids) even when runnig at
exactly same time (microseconds)?

is this enough ???

$r = mt_rand();
$uid = uniqid(getmypid() . $r);

thanks

Jan


Reply With Quote
  #2 (permalink)  
Old 07-26-2003
Tom Rogers
 
Posts: n/a
Default Re: [PHP] unique id

Hi,

Saturday, July 26, 2003, 5:58:41 PM, you wrote:
j> Hi there!

j> What's the best way to create uids (unique ids) even when runnig at
j> exactly same time (microseconds)?

j> is this enough ???

j> $r = mt_rand();
j> $uid = uniqid(getmypid() . $r);

j> thanks

j> Jan


I use:

$id = md5(uniqid(rand(),1));

--
regards,
Tom

Reply With Quote
  #3 (permalink)  
Old 07-26-2003
Curt Zirzow
 
Posts: n/a
Default Re: [PHP] unique id

* Thus wrote jan (jan@plazma.sk):
> Hi there!
>
> What's the best way to create uids (unique ids) even when runnig at
> exactly same time (microseconds)?
>
> is this enough ???
>
> $r = mt_rand();
> $uid = uniqid(getmypid() . $r);


If you're running a cluster of machines I would include information
on the machine that is generating the uid.

$uid = uniqid(getmypid() . $r . '_MACHINE_NAME_');

Curt
--
"I used to think I was indecisive, but now I'm not so sure."
Reply With Quote
  #4 (permalink)  
Old 07-28-2003
Curt Zirzow
 
Posts: n/a
Default Re: [PHP] unique id

* Thus wrote Gabriel Guzman (gabe@careercast.com):
> On Sat, 2003-07-26 at 07:11, Curt Zirzow wrote:
> > * Thus wrote jan (jan@plazma.sk):
> > > Hi there!
> > >
> > > What's the best way to create uids (unique ids) even when runnig at
> > > exactly same time (microseconds)?
> > >
> > > is this enough ???
> > >
> > > $r = mt_rand();
> > > $uid = uniqid(getmypid() . $r);

> >
> > If you're running a cluster of machines I would include information
> > on the machine that is generating the uid.
> >
> > $uid = uniqid(getmypid() . $r . '_MACHINE_NAME_');
> >
> > Curt

>
> this is similar to what we do as well:
>
> hexadecimal timestamp + hexadecimal pid + hexadecimal randomvalue + hex
> ip address of server (only used if using multiple machines, otherwise
> this part is left off).
>
> I've also considered using semaphores in my id generation function to
> insure only one counter can be created at a time, but am worried that
> this might cause a bit of a performance hit. Anyone tried this?


The only other way I have done it was to make a central database
with a table containing a auto incremented id. All scripts
would add a record and get back the auto incremented id number.
Since db inserts are atomic, it is guaranteed to be unique.

I think the biggest weak point in this approach is if the
connection goes down between the web server and db server, the
script is left clueless as what to do.


Curt
--
"I used to think I was indecisive, but now I'm not so sure."
Reply With Quote
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT +1. The time now is 02:55 AM.


Powered by vBulletin® Version 3.7.3
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0