Re: Unique random string
| From: | (Delbono) | Date: | Wed, 28 Jun 2000 00:34:22 +0000 |
| Subject: | Re: Unique random string | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-3483@lists.php.net to get a copy of this message | ||
Maybe this may appear a stupid answer but...
Why don't create a database table and use the auto_increment field as the
first part of the unique id..
the second part could be a randomly generated string of x chars...
You could do this for example.. (supposing MySql)
$second_part_alias = $rndstr; // a random string of desired characters
lenght
$query = "insert into first_part_aliases_table
values('','$second_part_alias')";
// then execute the query
$result = mysql_query($query, $lnk);
$first_part_alias = mysql_insert_id($lnk);// get last insert id
$unique_alias = $first_part_alias . "-" . $second_part_alias;
// with this you'll be absolutely sure that no duplicates will be generated
//
Note:
of course you have to estimate how many users will you may have in the next
decade... in order to generate a second_part string not too long
that if concatenated with first part, generates a string longer than 15
chars..
Supposing in the next decade people on the earth will be not more than 20
Billions
20,000,000,000 is a 11 chars long string (without commas) you'll create a
string of 3 chars to be concatenated
or
if you want to be sure the alias is 15 chars long you can get the
first_part_alias lenght (that varies if users increment) and calculate the
difference from 15, for the lenght of the string to be concatenated as the
second part...
A unique id with this method could be:
$ID = 34467-dgjGyy // for user number 34467
hope to be useful...
Nicola Delbono
----- Original Message -----
From: "William Ahern" <wahern@mailshell.com>
To: <php-general@lists.php.net>
Sent: Wednesday, June 28, 2000 1:34 AM
Subject: [PHP] Unique random string
> Any ideas on how I can generate a 'guaranteed' unique ascii string less
than
> 15 characters.
>
> It must be guaranteed unique across several independent
> servers.
>
> Currently we are generating random short strings to create unique aliases
for
> email addresses. The only problem is that to guarantee uniqueness we keep
a log
> of previous strings and check to make sure we don't create duplicates (we
cannot
> rotate strings. They must be absolutely unqiue for the next decade or so).
>
> The optimal solution would be to generate strings that are truly unique
and
> hopefully random enough, w/o having to go through the process of checking
for
> duplicates.
>
> I'd use uniqid(), except that it is 32 characters (too long since users
will
> have to handle them sometimes) and the algorithm must be available to C,
C++,
> Perl, whatever.
>
> Any comments, suggestions or answers! ;) would be great.
>
> Please add my address to the reply as I am not subscribed to the list.
>
> TIA,
>
> Bill
>
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, e-mail: php-general-unsubscribe@lists.php.net
> For additional commands, e-mail: php-general-help@lists.php.net
> To contact the list administrators, e-mail: php-list-admin@lists.php.net
>
>