Home Upgrade Search Memberlist Extras Hacker Tools Award Goals Help Wiki Contact

PHP 

How to generate unique key of random numbers in PHP

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.
Isn't mt_rand good enough or this haven't been suggested to you?
http://php.net/manual/en/function.mt-rand.php
You see, Doctor, God didn't kill that little girl. Fate didn't butcher her and destiny didn't feed her to those dogs.
If God saw what any of us did that night he didn't seem to mind. From then on I knew... God doesn't make the world this way.
We do.
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
http://php.net/manual/en/function.openss...-bytes.php
/* No comment */
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.
Life is a fig newton of you imagination! ~ https://www.brainwaveblitz.com/canyousol..._solve.php
(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
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 🤝
(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
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.


Possibly Related Threads...
Thread / Author Replies Views Last Post
[Javascript]  by blackcoder00Jun 4, 2020 11:26 PM 3 151 Jun 4, 2020 11:26 PM
Last Post: krychaj5
[PHP] by SkidyDec 11, 2017 11:02 AM 6 230 Dec 11, 2017 11:02 AM
Last Post: Charger
[PHP] by Vip3rrNov 15, 2017 11:30 PM 5 232 Nov 15, 2017 11:30 PM
Last Post: V3t3r4n