Re: Creating Unique Values
| From: | Sascha Schumann | Date: | Wed, 04 Oct 2000 17:13:16 +0000 |
| Subject: | Re: Creating Unique Values | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-18617@lists.php.net to get a copy of this message | ||
On Wed, 4 Oct 2000, Andy Woolley wrote:
> Thanks Andrew,
>
> I decided to use the function called erm.... the one that I can never
> remember what it's called without looking in the manual.
>
> microtime(); that's it.
>
> Heres what I did:
>
> $uniquevalue = microtime();
> $uniquevalue = ereg_replace ("\.", "", $uniquevalue); #get rid of the
> period.
> setcookie("cookieset", $uniquevalue);
>
> As microtime() takes it start time from the Unix Epoch (0:00:00 January 1,
> 1970 GMT) the chances of a duplicate are......shall we say slim.
The probability of a collision on your common UNIX system is
1/100, because the resolution of the timer is usually 100Hz.
That almost guarantees problems, if your application relies
on having unique ids.
A better approach is to use PHP's uniqid() and to enable the
lcg. Feed the result through md5 and the probability of a
collision is reduced significantly.
$id = md5(uniqid("mysite", 1));
- Sascha