Page 1 of 1

how do i genertae unique strings or for user identification.

Posted: Tue Jun 13, 2006 10:15 am
by pedrotuga
I need to assign a unique code, string or intiger, to each user.
The codes have to be absolutly unique and they can be used as a secret referer... for example, if the user X wants to trust user Y, he can just send him his secret code.

I think using a random string is the way to go. But how do i generate uique strings?
I think five or six character long would do it, more than that it can be anoying to the users.

Any ideas?

Thx in advance

Posted: Tue Jun 13, 2006 10:23 am
by TheMoose
http://www.php.net/uniqid

Given the example, you can cut it down to something like:

Code: Select all

// no prefix
$token = substr(md5(uniqid()), 0, 6);

// better, difficult to guess
$better_token = substr(md5(uniqid(rand(), true)), 0, 6);

Posted: Tue Jun 13, 2006 10:29 am
by pedrotuga
It looks like using substr() sacrifies the uniciticy of the string.
Ok.. thats not likely to hapen but... should i, just in case, query the database checking if that value exists already?

By the way, in case i do that i guess its recomended to use a btree index to that field right?

Posted: Tue Jun 13, 2006 11:58 am
by bdlang
What is the context? If this is in reference to storing user data in an RDBMS such as MySQL, you should be utilizing the built in MySQL AUTO_INCREMENT INT type field. Otherwise, you might find this previous thread useful.

Posted: Mon Jun 26, 2006 5:40 am
by pedrotuga
bdlang wrote:What is the context? If this is in reference to storing user data in an RDBMS such as MySQL, you should be utilizing the built in MySQL AUTO_INCREMENT INT type field. Otherwise, you might find this previous thread useful.
nope.. i need unique random strings in order to asign activation codes to the users... i am using the method pointed above.