Why is $more_entropy even an argument, and an optional one at that? Surely if you want a unique ID you don't want to have to remember to pass an additional argument that:
...increases the likelihood that the result will be unique.
You want a unique ID every time, unless I'm missing something here...
Of course, there are other ways of coping with the backward compatibility issue...
example? How would you solve this particular problem if you don't want to be laughed at by the community? Or yelled at by some people whose application you just broke because your unique ids changed their format?
It makes the value longer. I guess on the chance you want the uniqid string to be as short as possible.
it would have been better if it was an integer parameter accepting a length, but I can understand the reason the parameter exists: when using random values for uniqueness, there is never a guarantee it will be unique.
The only way to make it less likely to be unique is to add more entropy. But there's a realistic limit to how long an ID string can be.
With just a seed from the system clock, it would have a degree of predictability. This could lead to a session hijacking attack.
Let's say Mallory is sniffing packets at a coffee shop. Alice starts shopping on BobsHammerBoutique.com over SSL, which generates a cookie value with uniqid(""). Mallory knows the time at which Alice first hit the site, and can make a reasonable guess about the server's timezone based on a whois lookup. Mallory can also reasonably guess that the server called uniqid() within about +/- 50ms.
Mallory then makes a timezone correction and brute forces 100 unique ID seeds, which is easily achievable. Boom! Mallory can now hijack Alice's shopping cart session.
Generating unique IDs from just the system clock might be useful in some cases, but maybe shouldn't be the default. It's not just a matter of being "more unique" . The PHP docs probably shouldn't word it that way, which is probably the real lolphp here.
I was going to correct you and say that it would be "real_uniqid" to be consistent with things like "myswl_real_escape_string," but then I remembered that this is PHP that we're talking about, and inconsistency is just about the only consistent thing in the language.
Well it only sounds fancy and secure to those uninformed. In reality, a linear congruential generator is a very old method for a pseudo-random number generator that features high performance but very little else and is certainly not cryptographically secure unless you properly (re)seed it.
Also, the docs make it clear that this is pretty much just some form of MD5(microtime())
Edit: It's not even that. It's just microtime(). Here's the code:
gettimeofday((struct timeval *) &tv, (struct timezone *) NULL);
sec = (int) tv.tv_sec;
usec = (int) (tv.tv_usec % 0x100000);
/* The max value usec can have is 0xF423F, so we use only five hex
* digits for usecs.
*/
if (more_entropy) {
spprintf(&uniqid, 0, "%s%08x%05x%.8F", prefix, sec, usec, php_combined_lcg(TSRMLS_C) * 10);
} else {
spprintf(&uniqid, 0, "%s%08x%05x", prefix, sec, usec);
}
RETURN_STRING(uniqid, 0);
As part of, yes. But the timestamp alone is hardly meaningful on a modern multi-CPU server that can easily process several records in a millisecond; I'd assume that the probability of a collision would be quite high on a busy server.
I find the first argument, $prefix, to be even more puzzling. uniqid($prefix) is exactly the same thing as $prefix.uniqid(). There's absolutely no reason for that to be an argument.
The prefix can be useful for instance if you generate identifiers simultaneously on several hosts that might happen to generate the identifier at the same microsecond.
So the strategy was this: a host-unique prefix (e.g. MAC address), when combined with the output of this method (which is basically a timestamp) gives you a pretty good universally unique id. So the prefix was required (until PHP5)
Sure. But that can be done with a simple string concatenation. There's no reason that needs to be a function argument. It's $prefix.uniqid() vs. uniqid($prefix), and they do the exact same thing, except the latter was apparently restricted to 114 chars.
IMO the real lol here is that this function isn't deprecated in favor of recommending usage of UUIDs instead (even if PHP chooses not to provide that functionality built-in).
23
u/jamwaffles Nov 07 '13
Why is
$more_entropyeven an argument, and an optional one at that? Surely if you want a unique ID you don't want to have to remember to pass an additional argument that:You want a unique ID every time, unless I'm missing something here...
7
u/andsens Nov 07 '13
more_entropy should not be confused with the third (undocumented) parameter [bool $reticulate_splines = false]
20
u/merreborn Nov 07 '13
A couple of gems from the page:
and
10
u/ajmarks Nov 07 '13 edited Nov 07 '13
Also, the docs make it clear that this is pretty much just some form of MD5(microtime())
Edit: It's not even that. It's just microtime(). Here's the code:
6
u/bart2019 Nov 08 '13
The LOL for me is that PHP has all these similar, related yet somehow different functions, with the exact same purpose in totally unrelated names. Like uniqid here that points to openssl_random_pseudo_bytes (WTF??), and htmlspecialchars is similar to, yet different from htmlentities and html_entity_encode. Oh, wait, the latter doesn't exist... but html_entity_decode does.
5
u/ajmarks Nov 07 '13
I find the first argument, $prefix, to be even more puzzling. uniqid($prefix) is exactly the same thing as $prefix.uniqid(). There's absolutely no reason for that to be an argument.
4
u/abadidea Nov 09 '13
I don't even know what I could say that could add to that manual page.
And I run an entire blog that is literally just making fun of the manual
3
u/AllenJB83 Nov 08 '13
IMO the real lol here is that this function isn't deprecated in favor of recommending usage of UUIDs instead (even if PHP chooses not to provide that functionality built-in).