PHP Generate Unique ID

Discussion in 'PHP' started by Pudge1, Jan 11, 2011.

  1. #1
    I want to generate an ID consisting of alphanumeric characters ([a-z][A-Z][0-9]), but I need someway to make the ID as short as possible, for instance if there is still single digit IDs that have not been used yet I want those used first before it makes a 2 digit one and so on and so forth. I've thought about it and I just can't think of a way to do this. Anyone else know of a way?
     
    Pudge1, Jan 11, 2011 IP
  2. Tanya Roberts

    Tanya Roberts Active Member

    Messages:
    250
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    75
    #2
    Get the current TIMESTAMP :D
    the most unique thing in this whole world.
     
    Tanya Roberts, Jan 12, 2011 IP
  3. MyVodaFone

    MyVodaFone Well-Known Member

    Messages:
    1,048
    Likes Received:
    42
    Best Answers:
    10
    Trophy Points:
    195
    #3
    
    function genRandomString($length) {
        $characters = "0123456789abcdefghijklmnopqrstuvwxyz";
        $string = "";    
        for ($p = 0; $p < $length; $p++) {
            $string .= $characters[mt_rand(0, strlen($characters))];
        }
        return $string;
    }
    
    PHP:
    Called with:
    
    $string = genRandomString(5); // 5 represents the character length 
    
    PHP:
    You can then do a mysql query to make sure its not in use already, assuming you are storing ID's ?
     
    MyVodaFone, Jan 12, 2011 IP
  4. danx10

    danx10 Peon

    Messages:
    1,179
    Likes Received:
    44
    Best Answers:
    2
    Trophy Points:
    0
    #4
    well you can use microtime() with a variation of/and uniqid(), md5(), rand().
     
    danx10, Jan 12, 2011 IP
  5. Pudge1

    Pudge1 Well-Known Member

    Messages:
    912
    Likes Received:
    6
    Best Answers:
    1
    Trophy Points:
    140
    Digital Goods:
    1
    #5
    I can make random sequences. But I want it so it checks to see if the id is already taken and then generates the next id in the sequence. For instance 3 ids have been generated already the ids are (a, b and c). A new id is made and it is d.
     
    Pudge1, Jan 12, 2011 IP
  6. ssmm987

    ssmm987 Member

    Messages:
    180
    Likes Received:
    4
    Best Answers:
    3
    Trophy Points:
    43
    #6
    I assume they are stored somewhere?

    In that case, you can just take the last id, convert it to base 62 (10 numbers, 26 lower case, 26 uppercase) ,add one, and convert it back.
     
    ssmm987, Jan 12, 2011 IP
  7. Pudge1

    Pudge1 Well-Known Member

    Messages:
    912
    Likes Received:
    6
    Best Answers:
    1
    Trophy Points:
    140
    Digital Goods:
    1
    #7
    Thanks ssmm987! Anyone know how you can encode/decode base62 on PHP?
     
    Pudge1, Jan 12, 2011 IP
  8. kp_mastermind

    kp_mastermind Active Member

    Messages:
    885
    Likes Received:
    17
    Best Answers:
    0
    Trophy Points:
    58
    #8
    start with aaAA00 , covert to binary add 1, convert back.
     
    kp_mastermind, Jan 12, 2011 IP
  9. ssmm987

    ssmm987 Member

    Messages:
    180
    Likes Received:
    4
    Best Answers:
    3
    Trophy Points:
    43
    #9
    <?php
    function from62($base62)
    {
    	$base62=strrev($base62);
    	$base10=0;
    	$str="0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
    	for($i=0;$i<strlen($base62);$i++)
    	{
    		$base10+=(strpos($str,$base62{$i}))*pow(62,$i);
    	}
    	return $base10;
    }
    function to62($base10)
    {
    	$str="0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
    	$base62="";
    	$start=floor(log($base10,62));
    	for($i=$start;$i>=0;$i--)
    	{
    		$base62.=$str{floor($base10/pow(62,$i))};
    		$base10%=pow(62,$i);
    	}
    	return $base62;
    }
    $last="Asdf";
    $new=to62(from62($last)+1);
    PHP:
    change the $last variable to the last unique id given, and the $new variable will give you the next in line.
     
    ssmm987, Jan 12, 2011 IP
  10. loog12us

    loog12us Peon

    Messages:
    36
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #10
    use time stamp or create an auto number field in mysql
     
    loog12us, Jan 13, 2011 IP
  11. CPAPubMichael

    CPAPubMichael Peon

    Messages:
    231
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #11
    There is only base64 not base62 to my knowledge. Here, try this.

    
    
    <?php
    
     base64_encode($value);
    
    ?>
    
    
    PHP:
    Here is how you Decode.

    
    
    <?php
    
     base64_decode($value);
    
    ?>
    
    
    PHP:
    Hope i helped,

    Michael
     
    CPAPubMichael, Jan 13, 2011 IP
  12. ssmm987

    ssmm987 Member

    Messages:
    180
    Likes Received:
    4
    Best Answers:
    3
    Trophy Points:
    43
    #12
    Base 64 is used to encode text, and uses two additional characters, the slash and the equals sign, which cannot be passed through an url.

    But except from that: The base 64 encoding of the numbers 0 through 10 are:
    Which are not the shortest available id's
     
    ssmm987, Jan 13, 2011 IP