Aarav123 Posted October 17, 2016 Share Posted October 17, 2016 (edited) Hi, I am looking for some example code that can help me to generate unique random string of numeric numbers in PHP Applications. I am hoping to resolve this issue with the help of tech developers working around & for this I have researched for many php questions & answers in various forums, but didn't get any satisfactory response. Hoping to get answer here. Edited October 17, 2016 by requinix Quote Link to comment https://forums.phpfreaks.com/topic/302350-how-to-generate-unique-key-of-random-numbers-in-php/ Share on other sites More sharing options...
requinix Posted October 17, 2016 Share Posted October 17, 2016 You cannot have both random and unique: if it's completely random then there's a chance you'll generate the same string twice. But you can generate something unique that looks random, or you can pair something random with something unique into one string. What are your criteria for this string? Quote Link to comment https://forums.phpfreaks.com/topic/302350-how-to-generate-unique-key-of-random-numbers-in-php/#findComment-1538357 Share on other sites More sharing options...
Jacques1 Posted October 17, 2016 Share Posted October 17, 2016 A sufficiently big random number from a device like /dev/urandom will provide uniqueness, so this is perfectly valid. PHP has a large variety of randomness APIs depending on the version: If you have PHP 7, there are random_bytes() and random_int(). This is the recommended approach. The same functions are available in PHP 5 through a library. If you neither have PHP 7 nor want to use a library, there are mcrypt_create_iv() from the Mcrypt extension and openssl_random_pseudo_bytes() from the OpenSSL extension. To ensure global uniquess, you should generate 128 bits (16 bytes). You can then use hex-encoding or Base64-encoding to make the bytes human-readable. If you want a standardized format, there's UUID. <?php // Include the random_compat library if the PHP version is below 7 $random = random_bytes(16); // hex encoding $id = bin2hex($random); echo $id, '<br>'; // a shorter alternative: Base64 encoding (uses characters a-z, A-Z, 0-9, +, /) $id = rtrim(base64_encode($random), '='); echo $id, '<br>'; Quote Link to comment https://forums.phpfreaks.com/topic/302350-how-to-generate-unique-key-of-random-numbers-in-php/#findComment-1538358 Share on other sites More sharing options...
ginerjm Posted October 17, 2016 Share Posted October 17, 2016 Are you looking to create keys of some array or for table records? Or are you just looking for a single random number to use for some process? For the latter you have been provided answers. For the former, why? Just use a sequential number and then when you want to find one of them, use a random number function (limited by the max value already used) to select one of them. Quote Link to comment https://forums.phpfreaks.com/topic/302350-how-to-generate-unique-key-of-random-numbers-in-php/#findComment-1538365 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.