How to generate a random, unique, alphanumeric string in PHP (original) (raw)
Last Updated : 4 Jul, 2024
There are many ways to generate a random, unique, alphanumeric string in PHP which are given below:
Table of Content
- Using str_shuffle() Function
- Using md5() Function
- Using sha1() Function
- Using random_bytes() Function
- Using random_int() in a Custom Function
- Using uniqid() with more_entropy parameter
**Using str_shuffle() Function
The str_shuffle() function is an inbuilt function in PHP and is used to randomly shuffle all the characters of a string passed to the function as a parameter. When a number is passed, it treats the number as the string and shuffles it. This function does not make any change in the original string or the number passed to it as a parameter. Instead, it returns a new string which is one of the possible permutations of the string passed to it in the parameter.
**Example:
PHP `
`
Output
hnQVgxd4FE 6EsbCc53
**Using md5() Function
The md5() function is used to calculate the MD5 hash of a string. Pass timestamp as a argument and md5 function will convert them into 32 bit characters
**Example:
PHP `
`
Output
3de390c959 3de390c9
**Using sha1() Function
This function calculates the sha-1 hash of a string. Pass timestamps as a argument and sha1() function will convert them into sha1- hash.
**Example:
PHP `
`
Output
643f60c52d 643f60c5
**Using random_bytes() Function
This function generates cryptographically secure pseudo-random bytes. It returns a string containing the requested number of cryptographically secure random bytes. Use bin2hex () function to convert bytes into hexadecimal format.
**Example:
PHP `
`
Output
64713970f3 67b575a3
Using random_int() in a Custom Function
Using `random_int()` in a custom function generates a secure random alphanumeric string by selecting characters from a predefined set. It iterates to build a string of specified length, ensuring cryptographic security. Suitable for generating unique identifiers or secure tokens.
**Example:
PHP `
characterslength=strlen(characters_length = strlen(characterslength=strlen(characters); $random_string = ''; // Generate random characters until the string reaches desired length for ($i = 0; i<i < i<length_of_string; $i++) { randomindex=randomint(0,random_index = random_int(0, randomindex=randomint(0,characters_length - 1); randomstring.=random_string .= randomstring.=characters[$random_index]; } return $random_string; } // Generate and output random string of length 10 echo random_strings(10) . "\n"; // Generate and output random string of length 8 echo random_strings(8) . "\n"; ?>`
Output
asYSGGAKxe XxTNFXBN
Using uniqid() with more_entropy parameter
The uniqid() function in PHP generates a unique identifier based on the current timestamp with microseconds appended. By setting the optional more_entropy parameter to true, you can increase the uniqueness of the generated string by adding additional entropy.
**Example: This method uses uniqid() with more_entropy to create a random alphanumeric string. It's suitable for generating unique session IDs, temporary passwords, or other identifiers where uniqueness is critical. Adjust $length parameter as needed to generate strings of different lengths.
PHP `
randomString=substr(strreplace(′.′,′′,randomString = substr(str_replace('.', '', randomString=substr(strreplace(′.′,′′,prefix), 0, $length); return $randomString; } // Example usage $randomString = generateRandomString(6); echo $randomString; // Output: Random alphanumeric string of length 6 ?>`