Apr 12, 2016 PHP & MySQL Tutorial 44 - Generating unique IDs and random strings (uniqid and md5 functions). How to Create a Unique String in PHP Generate a Key. High Capacity PHP Random String. Strong Password Generator to create secure passwords that are impossible to crack on your device without sending them across the Internet, and learn over 30 tricks to keep your passwords, accounts and documents safe.
A unique user ID can be created in PHP using the uniqid function. Here are some examples of PHP code that's used to generate a random unique user ID. So if you want to generate string of a fixed length, you can either truncate the generated string or concatenate with another string, based on the requirement. Approach 3:Using uniqid function. The uniqid( ) function in PHP is an inbuilt function which is used to generate a unique ID based on the current time in microseconds (micro time). By default, it returns a 13 character long unique string.
Jun 22, 2013 There's a problem with guid: if I use guid I produce codes too long and complex to be written by an human. With my code I produce codes like that. All you're doing there is generating a default random number (so PHP doesn't have to parse any arguments) and chopping off the piece that's useful to you (using a bitwise operation which is faster than even basic math).
A unique user ID can be created in PHP using the uniqid () Thomson default key generator 2011 free download 32 bit. function. This function has two parameters you can set.
![Primary key Primary key](/uploads/1/2/5/8/125870425/566903212.jpg)
Php Random Function
![Generate Random Unique Key Php Generate Random Unique Key Php](/uploads/1/2/5/8/125870425/347235397.jpg)
The first is the prefix, which is what will be appended to the beginning of each ID. The second is more_entropy. If this is false or not specified, it will return 13 characters; if it's true, 23 characters will be returned.
Examples For Creating a Unique ID
Below are examples of creating a unique user ID, but each are a little different.
The first creates a normal unique ID while the second shows how to make a longer ID. The third example creates an ID with a random number as the prefix while the last line can be used to encrypt the username before storing it.
Here's my final version of a GUIDv4 function (based on others work here) that should work on all platforms and gracefully fallback to less cryptographically secure version if others are not supported..
<?php
/**
* Returns a GUIDv4 string
*
* Uses the best cryptographically secure method
* for all supported pltforms with fallback to an older,
* less secure version.
*
* @param bool $trim
* @return string
*/
function GUIDv4 ($trim = true)
{
// Windows
if (function_exists('com_create_guid') true) {
if ($trim true)
return trim(com_create_guid(), '{}');
else
return com_create_guid();
}
// OSX/Linux
if (function_exists('openssl_random_pseudo_bytes') true) {
$data = openssl_random_pseudo_bytes(16);
$data[6] = chr(ord($data[6]) & 0x0f | 0x40); // set version to 0100
$data[8] = chr(ord($data[8]) & 0x3f | 0x80); // set bits 6-7 to 10
return vsprintf('%s%s-%s-%s-%s-%s%s%s', str_split(bin2hex($data), 4));
}
// Fallback (PHP 4.2+)
mt_srand((double)microtime() * 10000);
$charid = strtolower(md5(uniqid(rand(), true)));
$hyphen = chr(45); // '-'
$lbrace = $trim ? ' : chr(123); // '{'
$rbrace = $trim ? ' : chr(125); // '}'
$guidv4 = $lbrace.
substr($charid, 0, 8).$hyphen.
substr($charid, 8, 4).$hyphen.
substr($charid, 12, 4).$hyphen.
substr($charid, 16, 4).$hyphen.
substr($charid, 20, 12).
$rbrace;
return $guidv4;
}
?>