0

I have an email address and I want to create a unique ID based on it, so say email is me@email.com that turns into 66wyy7eu

Ive found a close solution http://www.php.net/manual/en/function.uniqid.php#96898 but it needs the input to be numeric

daihovey
  • 3,485
  • 13
  • 66
  • 110

5 Answers5

2

emails are already unique.

You can't guarantee that a hash of the email will always be unique either.

If your using a DB. an auto-increment field will be unique

Jase
  • 599
  • 3
  • 9
  • ah but its for a url based on the email. but I dont want to use the email for privacy – daihovey Mar 29 '11 at 23:21
  • depending on how much traffic you get, an md5 or sha1 might be sufficient. High traffic will require a DB to track uniqueness – Jase Mar 29 '11 at 23:22
  • If emails are unique, why wouldn't a hashed email be unique as well? Provided you use the same hash algo... – Wesley Murch Mar 29 '11 at 23:23
  • because a hash is a limited amount of characters. You could hash a 1GB file but still get the same size hash as a 1byte string being hashed. Meaning eventually, the same hash can come about with 2 different inputs – Jase Mar 29 '11 at 23:24
  • @jase: that's not going likely to happen with sha1 and it's not going to happen at all with sha512 – dynamic Mar 29 '11 at 23:28
  • because they create much larger hashes. MD5 is much shorter and more likely to generate conflicts. I know the probability for each of a duplicate and yes sha1 and sha512 are unlikely to get conflicts but it's still possible. Experienced it a few times on datasets of 100,000+ records (code was written when the count was much smaller). Just for info. This was for generating api keys – Jase Mar 29 '11 at 23:31
  • if I used this hash("sha512") http://au2.php.net/manual/en/function.hash.php#101987 and limited it to 8 characters, would this be sufficient for less than 10000 users? – daihovey Mar 29 '11 at 23:32
  • limiting the hash size will just impair to the equivalent of md5. Just go with sha1. Think sha1 is still only 20 characters (something like that anyways) – Jase Mar 29 '11 at 23:36
  • yea I would not use md5 for this kind of thing as it has been proven to have collision factors – RobertPitt Mar 29 '11 at 23:36
  • I ended up using a hash with the autoincrement – daihovey Apr 04 '11 at 06:47
0

Check out hash(). This should allow you to generate a sufficiently unique ID based on a string input.

mfonda
  • 7,873
  • 1
  • 26
  • 30
0

have you read about md5 ? PHP md5 function

javment
  • 368
  • 1
  • 5
  • 17
0

Please see my answer to another question that is of the same nature, the function can be modified accordingly to suite your needs:

PHP random URL names (short URL)

as stated above the email addresses are unique, and if you store them into a database you will get a unique identification number from the Auto-increment column.

With that id you can then use the above function to create a unique hash for that id, and store that in the same row, then you have 2 identifiers for your email address, the ID to use internally and the encrypted key to use as a short URL service.

alternatively there is a simpler approach where as you constantly create random string and then check to see if it is within your database, if the key is within your database then you generate another and check again until you have a unique id.

here's a quick example:

function createRandomID($length = 9)
{
    $random = '';
    for ($i = 0; $i < $length; $i++)
    {
        $random .= chr(rand(ord('a'), ord('z')));
    }
    return $random;
}

and then simply do:

do
{
    $id = createRandomID();
}while(!idExists($id));

//Insert $id into our DB along with the email!

Note: The limitations of the characters effects the amount of unique strings it can produce, the more strings you have within your database the higher the loop rate becomes which could increase the load on your DB and result in slower pages for the user.

Community
  • 1
  • 1
RobertPitt
  • 56,863
  • 21
  • 114
  • 161
0

Personally, I would use something like md5() or sha1(). PHP does have a hash() function that allows you to specify the algorithm used: http://php.net/manual/en/function.hash.php