This will give you a random 8 digit string:
substr(str_pad(dechex(mt_rand()), 8, '0', STR_PAD_LEFT), -8);
Found here: http://www.richardlord.net/blog/php-password-security
Or, if the username field is unique, you can also use:
substr(md5('username value'), 0, 8);
Although this is highly unlikely, especially for md5, not a single case guarantees a unique string, so I will probably do something like this:
// Handle user registration or whatever... function generatePID($sUsername) { return substr(md5($sUsername), 0, 8); } $bUnique = false; $iAttempts = 0; while (!$bUnique && $iAttempts < 10) { $aCheck = $oDB->findByPID(generatePID("username value")); // Query the database for a PID matching whats generated if (!$aCheck) { // If nothing is found, exit the loop $bUnique = true; } else { $iAttempts++; } } // Save PID and such...
... which is likely to give only 1 "check" request, possibly 2 in unique cases and provide a unique string.
source share