SearchSearch   MemberlistMembers   RegisterRegister   ProfileProfile   Log inLog in 

Creating Unique Keys With PHP

 
Post new topic   Reply to topic    Open Hosting Internet Solutions Forum Index -> Articles and Tutorials -> Server Side Web Development
View previous topic :: View next topic  
Author Message
Andy T
Divine Being


Joined: 05 Aug 2002
Posts: 2425
Location: .mt

PostPosted: Wed Mar 08, 2006 2:16 pm    Post subject: Creating Unique Keys With PHP Reply with quote

Creating Unique Keys With PHP

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.

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.
Code:
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.
Back to top
View user's profile Send private message Visit poster's website
Display posts from previous:   
Post new topic   Reply to topic    Open Hosting Internet Solutions Forum Index -> Articles and Tutorials -> Server Side Web Development All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB 2.0.11 © 2001, 2002 phpBB Group
FAQClick here for help using the phpBB forum