Hack Forums
[PHP] How to generate unique key of random numbers in PHP - Printable Version

+- Hack Forums (https://hackforums.net)
+-- Forum: Tech (https://hackforums.net/forumdisplay.php?fid=444)
+--- Forum: Coding (https://hackforums.net/forumdisplay.php?fid=151)
+---- Forum: Web Development (https://hackforums.net/forumdisplay.php?fid=183)
+---- Thread: [PHP] How to generate unique key of random numbers in PHP (/showthread.php?tid=5436024)



How to generate unique key of random numbers in PHP - Ashish Bisht - Oct 17, 2016

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.


* - Squito - Oct 17, 2016

Isn't mt_rand good enough or this haven't been suggested to you?
http://php.net/manual/en/function.mt-rand.php


* - Nibor - Oct 17, 2016

Not sure what exactly you mean, but:

PHP Code

<?php 
$lenght 
= 10;
$string  = '';
for (
$i=1; $i<$lenght; $i++)
{
    
$string .= rand(0,9);
}
echo 
$string; 

This is not a secure method, though, for really storing important things etc


* - Quad - Oct 17, 2016

http://php.net/manual/en/function.openssl-random-pseudo-bytes.php


* - Strider64 - Oct 17, 2016

PHP Code

<?php 
private function generate_confirmation_number() {
        for (
$i = -1; $i <= 16; $i++) {
            
β = openssl_random_pseudo_bytes($i, $cstrong);
            
$hex = bin2hex(β);
        }

        return 
$hex;
    } 

is one way of doing it with OPENSSL.

There are true php random number generators libraries out there on the net, just Google and you should find some scripts or how to do it.


* - ASDA - Oct 18, 2016

(Oct 17, 2016 11:07 AM)Strider64 Wrote:  There are true php random number generators libraries out there on the net, just Google and you should find some scripts or how to do it.

lol good luck with that


* - Zotima - Oct 18, 2016

PHP Code

<?php 
function generateRandomString($length = 10) {
    
$characters = '0123456789';
    
$charactersLength = strlen($characters);
    
$randomString = '';
    for (
$i = 0; $i < $length; $i++) {
        
$randomString .= $characters[rand(0, $charactersLength - 1)];
    }
    return 
$randomString;
} 

This usually generates random string of numbers . You can edit the length of numbers too @ $length.

If you also want to add special chars and Alphabets just add each alphabet or char to the $charecters variable.

For example:

PHP Code

<?php 
$characters 
= '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ!@#$%^&*()'; 
This is probably the easiest and secure way of doing it. Hope this helps 🤝


* - ASDA - Oct 19, 2016

(Oct 18, 2016 12:31 PM)Yani Wrote:  You could use random_int() on PHP7 which is quite secure. It uses CryptGenRandom() on Windows and getrandom(2) on Linux, both of which are cryptographically safe. An official PHP5 userland implementation also exists.

cryptographically secure != true random though


* - F3nrir - Oct 23, 2016

PHP Code

<?php 
function randomName($length, $key = '') {
        
$keys = array_merge(range(0, 9), range('a', 'z'));
        for (
$i = 0; $i < $length; $i++) {
            
$key .= $keys[array_rand($keys)];
        }

        return 
$key;
    } 


I used this function in all my projects and it never disappointed.