how do i genertae unique strings or for user identification.

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
pedrotuga
Forum Contributor
Posts: 249
Joined: Tue Dec 13, 2005 11:08 pm

how do i genertae unique strings or for user identification.

Post 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
User avatar
TheMoose
Forum Contributor
Posts: 351
Joined: Tue May 23, 2006 10:42 am

Post 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);
User avatar
pedrotuga
Forum Contributor
Posts: 249
Joined: Tue Dec 13, 2005 11:08 pm

Post 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?
bdlang
Forum Contributor
Posts: 395
Joined: Tue May 16, 2006 8:46 pm
Location: Ventura, CA US

Post 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.
User avatar
pedrotuga
Forum Contributor
Posts: 249
Joined: Tue Dec 13, 2005 11:08 pm

Post 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.
Post Reply