unique ID

Discussion in 'PHP' started by Sky AK47, Aug 9, 2009.

  1. #1
    I'm trying to achieve a unique ID, like tinyurl, but currently having trouble.
    I have a code up and it works almost, it adds a new letter if needed, but it only changes the first letter and I want it to change everything.
    I have already tried by checking the next letter at first in the loop but failed, so I hope someone here can help me out.


    CODE REMOVED, I SOLVED IT
     
    Last edited: Aug 9, 2009
    Sky AK47, Aug 9, 2009 IP
  2. Pudge1

    Pudge1 Well-Known Member

    Messages:
    912
    Likes Received:
    6
    Best Answers:
    1
    Trophy Points:
    140
    Digital Goods:
    1
    #2
    I used to have a site like that but I sold it. Just make it randomly generate a like 6-7 digit code or something and then check to make sure it doesn't already exist. That other way is too complicated.
     
    Pudge1, Aug 9, 2009 IP
  3. Sky AK47

    Sky AK47 Member

    Messages:
    298
    Likes Received:
    8
    Best Answers:
    1
    Trophy Points:
    45
    #3
    But achievable, I don't want any random strings and also will get hard to get a random one after a big database.
    And yea it's hard, that's why I posted it here, as I couldn't get any further then this.
    Currently: 63+63+63 possibilities in place of 63*63*63
     
    Sky AK47, Aug 9, 2009 IP
  4. Zizzi

    Zizzi Peon

    Messages:
    67
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    If you don't mind the length just use an MD5 hash of userip + filename. You could even take the first 8 characters of the hash if you want. You don't necessarily need something as complicated as what you have there. Just make sure you have a way of checking to make sure you don't have any collisions.

    You could even do md5(time.userip.filename) and just use the first 8 chars. It's up to you!

    Using just the first 8 chars you will get about 4 trillion possible outputs.
     
    Zizzi, Aug 9, 2009 IP
  5. Sky AK47

    Sky AK47 Member

    Messages:
    298
    Likes Received:
    8
    Best Answers:
    1
    Trophy Points:
    45
    #5
    I want the hard way! :)
     
    Sky AK47, Aug 9, 2009 IP
  6. Pudge1

    Pudge1 Well-Known Member

    Messages:
    912
    Likes Received:
    6
    Best Answers:
    1
    Trophy Points:
    140
    Digital Goods:
    1
    #6
    I would recommend you just make a random string and then use a script to assign it to that URL. Your choice though.
     
    Pudge1, Aug 9, 2009 IP
  7. Sky AK47

    Sky AK47 Member

    Messages:
    298
    Likes Received:
    8
    Best Answers:
    1
    Trophy Points:
    45
    #7
    Thanks, but I really want this. Does anyone even have a idea on what to do? xD
     
    Sky AK47, Aug 9, 2009 IP
  8. jestep

    jestep Prominent Member

    Messages:
    3,659
    Likes Received:
    215
    Best Answers:
    19
    Trophy Points:
    330
    #8
    I think I would take a different approach to this. If you can remove the numbers you can just increment the string.

    
    
    $q = mysql_query("SELECT `url_key` FROM `url` ORDER BY `id` DESC LIMIT 1");
    
        if(mysql_num_rows($q) > 0)
        {    
    
        $last_array = mysql_fetch_array($q);
        $last_url = $last_array['url_key'];
        
        $new_url = $last_url++;
        } else {
        $new_url = 'a';
        }
    
    PHP:
    You wouldn't be able to use numbers but it should be a much simpler method of handling this.
     
    jestep, Aug 9, 2009 IP
  9. Sky AK47

    Sky AK47 Member

    Messages:
    298
    Likes Received:
    8
    Best Answers:
    1
    Trophy Points:
    45
    #9
    It's a great approach, but I already tried that and this doesn't work correctly with more then one character :(
    It gets close, but it wont change the first letter, zzz becomes aaaa while azz is never been used or am I wrong?.
    EDIT: YES, I think I got it. I've noted all the possibilities I've got with the code:
    aaa - 9aa
    aba - a9a
    b9a - 99a
    aab - aa9
    ba9 - 9a9
    ab9 - a99
    b99 - 999
    Is this everything?

    EDIT2: False result, I tried it again and it does everything, yay! XD I own
     
    Last edited: Aug 9, 2009
    Sky AK47, Aug 9, 2009 IP