help with random code generation
I'm trying to generate a random reference code for an auction style website.
Currently I'm using the following which generates a random ten character code.
*This is not my code i found it elsewhere on the net.
This is fine but what I really need is do is add 'DAD' before each random seven character code.
$random = substr(base64_encode(rand(1000000,9999999)),0,7);
So in other words my reference code would be ten characters long
Just wondering if anybody had any suggestions off the top of their heads.
Just figured it out
$random = 'DAD' . substr(base64_encode(rand(1000000,9999999)),0,7);
I don't know if this is an issue for what you are doing or not, but note that rand() only promises to generate a (pseudo) random number. It is not necessarily unique, so there is always a possibility that any value it generates will be a duplicate of a previously generated one. If this is an issue, you might look into using uniqid().
$random = "DDD" . md5(uniqid(''));
"Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
~ Terry Pratchett in Nation
How to Ask Questions the Smart Way
(not affiliated with this site, but well worth reading)
Hi Nog Dog,
Thanks for pointing this out to me.
I changed it and it worked fine.
Thanks for your help again.
Users Browsing this Thread
There are currently 1 users browsing this thread. (0 members and 1 guests)