Unique ID generaion script

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
shreehart
Forum Newbie
Posts: 1
Joined: Fri Feb 05, 2010 12:46 am

Unique ID generaion script

Post by shreehart »

script which will genrate 5-digit hexadecimal number?
dejvos
Forum Contributor
Posts: 122
Joined: Tue Mar 10, 2009 8:40 am

Re: Unique ID generaion script

Post by dejvos »

What about:

Code: Select all

 
srand(time());
$x = dechex(rand(1,16^5)-1);
$zero = "";
if(strlen($x) < 5)
for($i = strlen($x); $i<=5;$i++)
$zero .= "0";
 
$result = $zero.$x;
 
That could works.
dejvos
Forum Contributor
Posts: 122
Joined: Tue Mar 10, 2009 8:40 am

Re: Unique ID generaion script

Post by dejvos »

Actually I forgot to mention that the number will be pseudo-random. If you want a real random number, you need somthing else then ordinary computer.

Read here: http://www.random.org/
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Re: Unique ID generaion script

Post by VladSun »

dejvos wrote:What about:

Code: Select all

 
srand(time());
... 
It's vulnerable to bruteforce seed attack, which allows the attacker to predict the generated "random" strings.

We've discussed this recently - viewtopic.php?f=34&t=112311
There are 10 types of people in this world, those who understand binary and those who don't
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Re: Unique ID generaion script

Post by pickle »

shreehart wrote:script which will genrate 5-digit hexadecimal number?
Yes, they exist. Did you have any particular problem you needed help with? That's what we tend to do here - "help" rather than "do your work for you"
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
Post Reply