Forum Moderators: coopster

Message Too Old, No Replies

generate unique identifier for user.

         

xbl01234

8:59 am on Mar 29, 2008 (gmt 0)

10+ Year Member



Hi:
a built function uniqid() says it can genrate a unique id.
for example, using the following code:


<?php
$user_id = md5(uniqid(rand(), true));
?>

but my question is that the ids generated from the above code and from different date are 100% different each other.

for example:

21/01/08, id=iuuj1ll;
22/01/08, id=ppokjjk;
7/09/08, id=klkkk1;
and so.

PHP_Chimp

11:38 am on Mar 29, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



If you want a unique id then use uniqid [uk.php.net].
The id's generated are only unique they are not completely random.

If you want completly random id's then you will need to make your own function, as an md5 or sha1 will not help. As these will provide you with a fixed length string. You could chop an md5 string at a random position, but then you would need to check that this string is unique and not just random.

So are you after unique or random or both?

<edit>
You may also want to look at the user comment as there is

<?php
$better_token = uniqid(md5(rand()), true);
?>

As md5 is not unique i.e. different strings will give the same output result (it has collisions).

[edited by: PHP_Chimp at 11:41 am (utc) on Mar. 29, 2008]

xbl01234

9:21 am on Mar 30, 2008 (gmt 0)

10+ Year Member



Thanks a lot.