Here a Script i have developed to generate key for providing license to my product! it generates unlimited unique keys! <?php // License Key generator by Roopa $year = date('Y'); $month = date('m'); $second = date('s'); $hour = date('H'); $minute = date('i'); $day = date('d'); $system1 = ($year*$month*$day); $eicoef = rand(1,9999999); $system2 = ($hour+$minute+$second); $result = ($system1*$system2)*$eicoef; $div = rand(1,99); $key = $result/$div; echo "Generated is Key is ". floor($key); ?> PHP: Well here is another one by n3r0x <?php function GetKey($length) { $x=0; $char = "abcdefghijklmnopqrstuvxyz=#1234567890-_"; $char_length = strlen($char); $str = ""; while($x++ < $length) { $str .= $char{rand(0,$char_length-1)}; } return $str; } echo "Key is ".GetKey(17); // Sample length Size is 17 ?> PHP: Feedback are welcomed!
As yours is based on the time/date theirs a php internal function which exists which has similar functionality take alook at uniqid()
Ah so you have numeric keys.. I use random strings like: function GetKey($length) { $x=0; $char = "abcdefghijklmnopqrstuvxyz=#1234567890-_"; $char_length = strlen($char); $str = ""; while($x++ < $length) { $str .= $char{rand(0,$char_length-1)}; } return $str; } PHP: Been using the same function to generate Cookie ID's for a long time.. never had a problem with duplicate keys..
Agreed. Have been using this kind of generator for a while now and have never had any duplicates, though the requirements may differ depending on the application itself - some might need to have a static algorithm so that there would be no need to connect to a remote server or store all of the keys in a local file to verify their validity.
Well this is unlikely to generate two identical keys unless they're generated at the exact same time, and it's a whole lot less code sha1(time()); PHP:
For the time() functions, if you're worried, use microtime instead. I'm not going to say it's impossible, but the realistic chances of getting 2 visitors with the same microtime would be very slim. Also, rand is pretty much a bad function. I would definitely recommend using mt_rand instead.
Yeah I did think of adding microtime but thought I'd show the simplest of examples. With microtime it's near impossible for two to produce the same time
Yep! i have wrotre thsi to generate only a key not for cookie any way will have a write in that as you said! Welcome! Feedbacks appreciated!
True but depends on the usage.. since i use mine for a "Session id" that updates each time the user enters the website that would mean that no 2 people would be allowed to enter the same second.. would make it useless on larger sites.. could user microtime() however
Its simple, no need to do that much program..., Have a look..i will show you one. md5(uniquid(rand())); in fact this will generates some random value, encrypt too.., but this will generates.., as many as possible. ----------- based on time date is awesome idea...., but are you sure it will generates unique keys,
It's not true that your code will "encrypt" the value, since for something to be an encryption, it also has to be possible to decrypt it, and a hash is one way only. Also MD5 statistically is far more likely to produce two identical hashes than SHA, which is why people are moving away from MD5