0

I want to generate unique key like '7HzdUakp'.

I want to put this in database (mysql) but i want to an almost infinite combinations.

I can generate it using random function but sometimes it can generate the same key twice

SOLVED - I generate it based on "id", view my answer bellow for more details

Łukasz Rząsa
  • 171
  • 3
  • 10
  • `echo uniqid();` or `echo uniqid(null, true);` for a bigger number – Antonios Tsimourtos May 12 '17 at 13:19
  • what do you mean by `with repeatability` ? – YvesLeBorg May 12 '17 at 13:19
  • 1
    http://stackoverflow.com/questions/4356289/php-random-string-generator – Saty May 12 '17 at 13:22
  • @Saty here SO want an `unique` random key – LF00 May 12 '17 at 13:24
  • dont roll your own, also beware of 'pseudo randomness. Look at **[this question and answers](http://stackoverflow.com/questions/4356289/php-random-string-generator/31107425#31107425)** for some pretty solid/safe/unique key generation. – YvesLeBorg May 12 '17 at 13:26
  • I think you're asking for a cryptographically secure (meaning: difficult to guess) random token for each post. I think you're asking for tokens *without* repeatability, not *with* repeatability. Right? Can you give us an estimate of how many posts will have these random tokens? It's important not to get caught by the so-called *birthday paradox,* which you can look up. So the number of posts has some bearing on the length of the token. Please [edit] your post to provide more details. And read this: http://stackoverflow.com/questions/4356289/php-random-string-generator/31107425#31107425 – O. Jones May 12 '17 at 13:49
  • Possible duplicate of [PHP random string generator](http://stackoverflow.com/questions/4356289/php-random-string-generator) – lax1089 May 12 '17 at 15:23

3 Answers3

0

Here's how i do it :

private function generateKey()
{
    $alphabet = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890';
    $key = array();
    $alphaLength = strlen($alphabet) - 1;
    for ($i = 0; $i < 8; $i++) {
        $n = rand(0, $alphaLength);
        $key[] = $alphabet[$n];
    }
    return implode($key);
}
Erwan Haquet
  • 263
  • 1
  • 11
0

You can use uniqid if you don't need to generate a cryptographically secure id. If you need something more "secure" use random_bytes($len);

Riccardo Bonafede
  • 610
  • 1
  • 9
  • 17
0

After hours i created this: (javastript for testing)

It's looks like random but isn't
- char_keys contains 8 rows with 60 unique chars
- helper_arr contains digits from id - function param (in reverse order) "5342" => [2,4,3,5]
- it can create 100 000 000 unique keys (with many combinations), but now i working to upgrade this (different key long)

Random generate works like this:
- for example helper_arr = [1,2,3,4,5];
- key is a generated string key

helper_arr[0] = 1, so:
- key[0] = char_keys[1].substr(0,1), or
- key[0] = char_keys[1].substr(10,11), or
- key[0] = char_keys[1].substr(20,21), or
- key[0] = char_keys[1].substr(30,31), or
- key[0] = char_keys[1].substr(40,41), or
- key[0] = char_keys[1].substr(50,51), or
- key[0] = char_keys[1].substr(60,61).
(this is the only random thing ;) )

test = function(id){
    var char_keys = [
        '123456789',//only numbers here because it's random, users can create own links without number on first char 
        'LwFhy7YuEWGMH1IvoU85Jbjpkx4qN6zBmf9KtercZdQiRln2CSDXaVg3T0Ps', 
        'GTxkHtjXbB08ZWYqyiIu2agvdAlVFM6ShQznEcJrm47pU5sCLKfNP9eow1R3',
        '8QEmj3LeRdWuKsJngiBySf1Xz6wpVZ0Nkv2GFPHah7UMqAboYtlcT9rCx4D5', 
        'G5QdYeSl0LFBjJAHCD1xnmU9VfZRi6MX2PcwsaKoqT8p3vtNhWz7u4ybgrEI', 
        'j7fCcZNF6ytv58KUET1BAiMR2JqH0eIadrhQzug9kbmDnPSGLo4xXVlWpY3s', 
        'PyAMjsiox86zZ1vYCDwETm5SQlGK9b0rLJNV2aFtgXe4cW7IhqUpRfH3unBk',
        'h04edQN2c89xVSiCMBlw3LksWEKaUguGj6mDIvTJfRpPH1AozZyYnFrq75tX',
        '9YD82VBwJ0PrIUNG5Sx4dHX6vz1kZgWaRs7plFhfAuojytqbiCmQcKMeL3nT' 
    ];
    var id = id;
    var tmp = 0;
    var helper_arr = [];
    while(id>0){
        tmp = Math.floor(id/10);
        if(tmp==0) {helper_arr.push(id-(tmp*10)); break;}
        helper_arr.push(id-(tmp*10)+1);
        id=tmp;
    } 
    var key = char_keys[0].substr(getRandomInt(0,8),1);
    for(i=1;i<helper_arr.length;i++){
        key += char_keys[i].substr((getRandomInt(1,6)*helper_arr[i])-1,1);
    }
    for(i=helper_arr.length;i<char_keys.length;i++){
        key += char_keys[i].substr((getRandomInt(1,6)*1)-1,1);
    }
    return(key);
}
Łukasz Rząsa
  • 171
  • 3
  • 10