how can i use rand to genrate a minimum 7 digit id number???

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
djdon11
Forum Commoner
Posts: 90
Joined: Wed Jun 20, 2007 5:03 pm

how can i use rand to genrate a minimum 7 digit id number???

Post by djdon11 »

Hi friends....

i want to give a unique no. id to my site's user .. for this i used the rand() function of php like this->

Code: Select all

$work_id = rand(1,9).rand(2,9).rand(3,9).rand(4,9);
here the $work_id generating a four digit no .. but the problem is that , some times it gives same id for two users randomly like 3439,3439 same for two users .. :(

how can i generate unique id for all of my users .... please help me...

Thanks !
User avatar
N1gel
Forum Commoner
Posts: 95
Joined: Sun Apr 30, 2006 12:01 pm

Post by N1gel »

For less chance off two being the same you could try a larger number. here is an example off using rand to generate a 10 digit number

Code: Select all

$work_id = rand(1000000000,9999999999);
If you are using a db could you not just have it as an auto_increment field.
User avatar
Mordred
DevNet Resident
Posts: 1579
Joined: Sun Sep 03, 2006 5:19 am
Location: Sofia, Bulgaria

Post by Mordred »

It looks like you're trying to reinvent sessions. Don't. Use sessions instead ;)
Post Reply