Web Hosting Talk







View Full Version : Random Numbers


vision3
01-10-2004, 08:51 PM
Hi guys
Im trying to find a way to generate a random number and words like: 53d43bk43 . Something like this. The reason i need this is because i want to create a script that went a user wants to buy a certificate online they will pay thru paypal and wend payment is made it will return them back to an IPN file to confirm the payment and email will be sent the user and the owner whith all the info inlcuding the random number. I'm already in the process of making this happen i just need to know how to go about making this random number thing.

Any help is wecome..thanks :)

brendandonhu
01-10-2004, 08:58 PM
If you use random numbers...its possible to have 2 customers get the same random number....if you want something unique, I would put the user's IP address and the current timestamp together. Then use the MD5 sum of that. It would look like this in PHP.

<?php
echo md5($_SERVER['REMOTE_ADDR'] . time());
?>

vision3
01-11-2004, 01:21 AM
hmm so this will solve not having both customers not get the same number?

brendandonhu
01-11-2004, 01:24 AM
Yes, unless one person buys your product twice within one second.

vision3
01-11-2004, 01:31 AM
lol thanks

hiryuu
01-11-2004, 07:21 AM
Originally posted by brendandonhu
Yes, unless one person buys your product twice within one second.
If that does come up often, php also has the gettimeofday() function, which will get you down to microseconds.

brendandonhu
01-11-2004, 11:27 AM
Yep, you could use that or microtime() too.

catweazel
01-12-2004, 09:38 AM
AFAIK a hash does not guarantee uniqueness. It definitely will not be unique if there are more input bits than the number of bits in the hash. If you need a unique string you could add a user ID to the end of a hash or random number.

vision3
01-12-2004, 10:27 AM
Originally posted by catweazel
AFAIK a hash does not guarantee uniqueness. It definitely will not be unique if there are more input bits than the number of bits in the hash. If you need a unique string you could add a user ID to the end of a hash or random number.

How do i go about adding a unique ID number?

brendandonhu
01-12-2004, 04:26 PM
But an IP address is a finite number of bytes....combining that with a timestamp ought to be quite unique. IMO the only way to get truly unique IDs would be give the first person ID 1, the second person ID 2, third person ID 3, etc.

StreetWarz
01-12-2004, 05:31 PM
you can always use both md5 and time..

example.. 01122004HASHHERE

so you know the exact date/special code..

:) just incase of duplications..

brendandonhu
01-12-2004, 06:16 PM
Um, thats what I posted....see my code above.

catweazel
01-12-2004, 08:50 PM
You could encrypt the IP and time.

ArcticKid
01-12-2004, 10:56 PM
This may be the cheasiet way, but this has worked for me too:


<?
function makeRandomPassword() {
$salt = "abchefghjkaremnpqrstuvwxyz0123456789";
srand((double)microtime()*1000000);
$i = 0;
while ($i <= 7) {
$num = rand() % 33;
$tmp = substr($salt, $num, 1);
$pass = $pass . $tmp;
$i++;
}
return $pass;
}
$random_password = makeRandomPassword();
echo "YOUR ID IS:.";
?>

It is pretty much a random password generator, but no one will know. Just make it a PHP Document, and make them use the password given.

vision3
01-13-2004, 12:26 AM
hmm cool ill play with that.