Page 1 of 1

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

Posted: Thu Oct 11, 2007 1:26 am
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 !

Posted: Thu Oct 11, 2007 4:29 am
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.

Posted: Thu Oct 11, 2007 5:32 am
by Mordred
It looks like you're trying to reinvent sessions. Don't. Use sessions instead ;)