Creating Unique Keys With PHP

Written by Andy Towler published 8th Mar 2006 | Comment on this article

If you script using databases, you will appreciate the need for unique key strings. There are articles all over the web showing you how it?s done in ASP, now here?s the lowdown for all you PHP pioneers.

Creating Unique Keys With PHP

There are various situations in which you may need a supply of guaranteed unique strings - for database index keys, user ids, etc. In ASP you can generate them quite easily and there are articles all over the web showing you how. In PHP it's quite easy too, and can be achieved by means of a small function.

The most common form of unique key string is a GUID (Globally Unique IDentifier), which is a 128-bit number, usually expressed as a string of 32 Hex digits. It's generated from the name of the computer it's running on, coupled with the system time. That's the kind of key we're going to generate here.

When running on a Windows server, PHP is quite good at interfacing with Windows COM libraries, and there's a useful one which will generate as many GUIDs as you can shake a large stick at, called 'Scriptlet.TypeLib'.

When running on a non-Windows server (e.g. Linux), this is obviously not available, however the PHP documentation provides a uniqid() function, which when combined with the md5() hashing function gives a pretty close approximation to a GUID, being based on the computer time in microseconds.

As OpenHosting runs PHP on Windows, my function attempts to use the Windows method first, as this is "guaranteed" to generate unique IDs (i.e. Microsoft rely on it to generate primary keys for database tables). If the COM object can't be created, the function falls back on the method described in the PHP user documentation, which ought to be unqiue enough for most purposes (if being "unique enough" is not an oxymoron!).

The function (getGuid) is shown below, along with a small supporting function (allowChars) to strip unwanted characters from a string. This is used to remove the { } and dashes that are part of a Windows-generated GUID.

function allowChars($strValue, $strChars, $blnCaseSense)
{
$strResult = "";
if ($blnCaseSense)
{
for ($i = 0; $i < strlen($strValue); $i++)
{
$strChar = substr($strValue, $i, 1);
if (is_numeric(strpos($strChars, $strChar))) { $strResult .= $strChar; }
}
}
else
{
$strChars = strtoupper($strChars);
for ($i = 0; $i < strlen($strValue); $i++)
{
$strChar = substr($strValue, $i, 1);
if (is_numeric(strpos($strChars, strtoupper($strChar)))) { $strResult .= $strChar; }
}
}
return $strResult;
}


function getGuid()
{
#
# First try and use the Windows-specific guaranteed method
#
if (!$objGuid = @new COM("Scriptlet.TypeLib"))
{
#
# Couldn't create the object (non-Windows server) so use the other method
#
$strGuid = md5(uniqid(rand(), 1));
}
else
{
#
# Created the object so use the GUID property and strip unwanted chars
#
$strGuid = allowChars(strtolower($objGuid->GUID()), "0123456789abcdef", false);
}
$objGuid = null;
return $strGuid;
}

All you have to do is put this function in a common library file somewhere, include it on a page, and you'll get a fresh unique 32-character string every time you call it.