PDA

Click to See Complete Forum and Search --> : Generating a random 10 digit number


Albatross
Jul 21st, 2007, 04:41 AM
Mornin all, I have what should be an easy q today...

I have a form that a user fills out and when they hit submit, it does a number of things, one, it checks to see if the email address supplied is already in the db (in which case it kicks back an error n halts), otherwise, it comes back with a visual confirmation showing all data and loads the information into the db as a new record.

What I'm trying to do is to have it so that a unique, 10 digit number is autogenerated and attached to the same record that's returned and also loaded to the db with that same record. A serial number would be a fair description.

Any ideas?

penagate
Jul 21st, 2007, 06:27 AM
Which part are you stuck with? Generating the number, or storing it in the database?

dclamp
Jul 22nd, 2007, 02:34 PM
well there are a few ways at doing that.

1. One large rand():


$serial = rand(1111111111, 9999999999);


That will make a random 10 digit string.

2. You can have several rand()'s and make it more random:


$serial = rand(111, 999) . rand(111,999) . rand(11, 99) . rand(11, 99);

DarkX_Greece
Aug 3rd, 2007, 02:57 AM
dclamp sollution is very nice but you still didn't answer if you have problem only with the random number or when storing it in database!

Also there are 2 other sollutions:

1)
srand((double)microtime()*1000000);
echo rand(1000000000,9000000000);

2)
$value_s = mktime(0,0,0,date('d'),date('m'),date('Y'));
echo $value_s;

mfurqan
Aug 20th, 2007, 08:48 AM
Yep. Try to use rand(); function.


$whatever = rand(1000000000, 9999999999);


If you want a random 10-Digit string then:


$token = substr(md5(rand(1000000000, 9999999999)), 0, 10);