Yes, the keys are stored. So far I've had about 6K of them but this is a good wake-up call for me. What I'll do is run the script, generate ID, query db for such value and if not found -- use it, if it is found, call the function again and repeat the steps.

Thanks for confirming my thought trend.

    Of course, there's a possibility, especially as more values get used, that you could get a sequence of several duplicates while trying to generate one of these, resulting in several database queries.

      I have yet to come across a duplicate when using the MySQL equivalent to UUID()

      /**
         *  uuid
         *
         *  uuid() simply replicates MySQL's UUID function, which returns a 36
         *  character "random" hash (32 alphanumeric, 4 dashes).
         *  @return string
         */
        function uuid(){
          return sprintf('%04x%04x-%04x-%04x-%04x-%04x%04x%04x',
            mt_rand(0, 0xffff), mt_rand(0, 0xffff), mt_rand(0, 0xffff),
            mt_rand(0, 0x0fff) | 0x4000,
            mt_rand(0, 0x3fff) | 0x8000,
            mt_rand(0, 0xffff), mt_rand(0, 0xffff), mt_rand(0, 0xffff));
        }
      

      Laserlight and I have discussed this before and there is always a chance of getting a duplicate ... but using UUID(), there are more combinations than known stars in the universe.

        18 days later

        I need a human readable key, with specified str length. What would the key look like in your code example?

          OK, I'm working myself into a loop...

          Here are the steps I need to accomplish:

          1. Generate key
          2. Check if the key exists
            if YES -- repeat step 1 and 2
            if NO -- use the key

          Here's how I generate the key:

          function generator($lenght=5)
          {
               for($i = 0; $i < $lenght; $i++)
               {
                    $uniqueKey .= chr(rand(97,122));
               }
               return $uniqueKey;
          }
          
          $randomKey = generator(); 
          

          Here's how I check if the key is stored in db:

          if ($randomKey == $db->get_var(“SELECT key FROM t1 WHERE key = '".$randomKey."' ") )
          {
          	// found match	-- re-generate key		
          } else {
          	// did not find match -- use key	
          }
          

          Need help putting it together without writing endless loops...

            testkey($key){
            if ($randomKey == $db->get_var(&#8220;SELECT key FROM t1 WHERE key = '".$randomKey."' ") )
            {
              $randomKey = generator(); 
            testkey($randomKey);
            } else {
            
            // did not find match -- use key    
            }
            }
            

              Dah... coffee. i need more coffee.

                the problem is he wanted one 5 chars long.

                i use md5() on either mysql's UUID() or php's uniqid

                but if you want a human to use it, it's not very friendly.

                  And the problem is not circumventable with 5 characters. I do not know how many keys he has, or expect to have. But with a 5 character key, collisions will start to become somewhat frequent at about 3500 existing keys; see Laserlight's post above.
                  Moving up to an 8 character key, this threshold is moved up to about 450000 keys. A decent improvement.

                    dagon;10936324 wrote:

                    the problem is he wanted one 5 chars long.

                    i use md5() on either mysql's UUID() or php's uniqid

                    but if you want a human to use it, it's not very friendly.

                    Granted the OP's default value for his function is 5 characters, I didn't see anywhere where the OP stated it had to be 5 characters, so I suggested UUID.

                    If you are going to have a substantial amount of users, you are not going to want a 5 or 8 character key for them. Just imagine you are the one in a million chance that your site gets as popular as Facebook is or MySpace was. I highly doubt the numbers 5 or 8 entered their mind for a user key.

                    But ... to each his own.

                      Kudose;10936408 wrote:

                      Granted the OP's default value for his function is 5 characters, I didn't see anywhere where the OP stated it had to be 5 characters, so I suggested UUID.

                      If you are going to have a substantial amount of users, you are not going to want a 5 or 8 character key for them. Just imagine you are the one in a million chance that your site gets as popular as Facebook is or MySpace was. I highly doubt the numbers 5 or 8 entered their mind for a user key.

                      But ... to each his own.

                      agreed, but he said 'human readable' so the assumption i was working with was 5 would be the default, or at least to few characters to use uuid. Humans are so annoying!

                        8 days later

                        The key is a part of a unique URL. It has to be short because users often write that URL down. And yes, humans can be difficult sometimes.

                          6 months later

                          I have a new case where I could use long unique IDs for naming images. I gave it a whirl and although the images are processed correctly and values stored in DB OK, I can't seem to be able to access images online with such name. Are there naming conventions that prevent such naming for images?

                          Kudose;10934541 wrote:

                          I have yet to come across a duplicate when using the MySQL equivalent to UUID()

                          /**
                             *  uuid
                             *
                             *  uuid() simply replicates MySQL's UUID function, which returns a 36
                             *  character "random" hash (32 alphanumeric, 4 dashes).
                             *  @return string
                             */
                            function uuid(){
                              return sprintf('%04x%04x-%04x-%04x-%04x-%04x%04x%04x',
                                mt_rand(0, 0xffff), mt_rand(0, 0xffff), mt_rand(0, 0xffff),
                                mt_rand(0, 0x0fff) | 0x4000,
                                mt_rand(0, 0x3fff) | 0x8000,
                                mt_rand(0, 0xffff), mt_rand(0, 0xffff), mt_rand(0, 0xffff));
                            }
                          

                          Laserlight and I have discussed this before and there is always a chance of getting a duplicate ... but using UUID(), there are more combinations than known stars in the universe.

                            I cannot think of a reason an image named XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX.jpg would not load.

                            What happens when you try to access the image address directly?

                              If the entire URI is less than 255 or so characters, then no, the length won't be a problem.

                                Write a Reply...