1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

Random Key Generator

Discussion in 'PHP' started by roopajyothi, May 18, 2010.

  1. #1
    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!
     
    Last edited: May 18, 2010
    roopajyothi, May 18, 2010 IP
  2. danx10

    danx10 Peon

    Messages:
    1,179
    Likes Received:
    44
    Best Answers:
    2
    Trophy Points:
    0
    #2
    As yours is based on the time/date theirs a php internal function which exists which has similar functionality take alook at uniqid()
     
    danx10, May 18, 2010 IP
  3. n3r0x

    n3r0x Well-Known Member

    Messages:
    257
    Likes Received:
    4
    Best Answers:
    1
    Trophy Points:
    120
    #3
    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..
     
    n3r0x, May 18, 2010 IP
  4. Mr. Bin

    Mr. Bin Peon

    Messages:
    3
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    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.
     
    Mr. Bin, May 18, 2010 IP
  5. roopajyothi

    roopajyothi Active Member

    Messages:
    1,302
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    80
    #5
    Umm..
    Now only had a look here!
    Thanks Danx for that :)
     
    roopajyothi, May 18, 2010 IP
  6. taylana

    taylana Member

    Messages:
    169
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    38
    #6
    Best method using date, time. Thanks for sharing.
     
    taylana, May 18, 2010 IP
  7. JAY6390

    JAY6390 Peon

    Messages:
    918
    Likes Received:
    31
    Best Answers:
    0
    Trophy Points:
    0
    #7
    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:
     
    JAY6390, May 18, 2010 IP
  8. roopajyothi

    roopajyothi Active Member

    Messages:
    1,302
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    80
    #8
    Oh! i will have a try!

    Welcome! :)
     
    roopajyothi, May 18, 2010 IP
  9. jestep

    jestep Prominent Member

    Messages:
    3,659
    Likes Received:
    215
    Best Answers:
    19
    Trophy Points:
    330
    #9
    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.
     
    jestep, May 18, 2010 IP
  10. JAY6390

    JAY6390 Peon

    Messages:
    918
    Likes Received:
    31
    Best Answers:
    0
    Trophy Points:
    0
    #10
    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
     
    JAY6390, May 19, 2010 IP
  11. ststacytucker9

    ststacytucker9 Peon

    Messages:
    15
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #11
    Really nice method thanks for sharing it
     
    ststacytucker9, May 19, 2010 IP
  12. roopajyothi

    roopajyothi Active Member

    Messages:
    1,302
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    80
    #12
    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! :)
     
    roopajyothi, May 19, 2010 IP
  13. remember123

    remember123 Peon

    Messages:
    620
    Likes Received:
    12
    Best Answers:
    0
    Trophy Points:
    0
    #13
    Clean code..
    thanks for sharing ;)
     
    remember123, May 26, 2010 IP
  14. roopajyothi

    roopajyothi Active Member

    Messages:
    1,302
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    80
    #14
    Welcome!
    I hope if you have any thing you can also share here! :)
     
    roopajyothi, May 27, 2010 IP
  15. n3r0x

    n3r0x Well-Known Member

    Messages:
    257
    Likes Received:
    4
    Best Answers:
    1
    Trophy Points:
    120
    #15
    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
     
    n3r0x, May 27, 2010 IP
  16. JAY6390

    JAY6390 Peon

    Messages:
    918
    Likes Received:
    31
    Best Answers:
    0
    Trophy Points:
    0
  17. n3r0x

    n3r0x Well-Known Member

    Messages:
    257
    Likes Received:
    4
    Best Answers:
    1
    Trophy Points:
    120
    #17
    lol missed that one.. sorry
     
    n3r0x, May 27, 2010 IP
  18. roopajyothi

    roopajyothi Active Member

    Messages:
    1,302
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    80
    #18
    Right i will soon update one with Microtime :)
    Thanks Jay & n3r0x
     
    roopajyothi, Jun 17, 2010 IP
  19. strgraphics

    strgraphics Active Member

    Messages:
    710
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    53
    #19
    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,
     
    strgraphics, Jun 19, 2010 IP
  20. JAY6390

    JAY6390 Peon

    Messages:
    918
    Likes Received:
    31
    Best Answers:
    0
    Trophy Points:
    0
    #20
    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
     
    JAY6390, Jun 19, 2010 IP