How to generate a random unique alphanumeric string in php?
PHP 7 standard library provides the Show
Example:
The above example will output something similar to:
More info: http://php.net/manual/en/function.random-bytes.php PHP 5 (outdated) I was just looking into how to solve this same problem, but I also want my function to create a token that can be used for password retrieval as well. This means that I need to limit the ability of the token to be guessed. Because
A quick, short and to the point answer is:
which will generate a random string of alphanumeric characters of length = $bytes * 2. Unfortunately this only has an alphabet of
Below is the strongest function I could make that satisfies the criteria (This is an implemented version of Erik's answer).
Source: http://us1.php.net/manual/en/function.openssl-random-pseudo-bytes.php#104322
gagarine 3,9632 gold badges29 silver badges39 bronze badges answered Dec 5, 2012 at 22:25
ScottScott 11.7k4 gold badges26 silver badges48 bronze badges 30
If you do not need it to be absolutely unique over time:
Otherwise (given you have already determined a unique login for your user):
answered Dec 4, 2009 at 10:55
loletechloletech 3,6521 gold badge14 silver badges3 bronze badges 6 Object-oriented version of the most up-voted solutionI've created an object-oriented solution based on Scott's answer:
Usage
Custom alphabetYou can use custom alphabet if required. Just pass a string with supported chars to the constructor or setter:
Here's the output samples
I hope it will help someone. Cheers! answered Jun 4, 2014 at 9:46
Slava Fomin IISlava Fomin II 24.6k27 gold badges116 silver badges194 bronze badges 5 I'm here with some good research data based on the functions provided by Scott's answer. So I set up a Digital Ocean droplet just for this 5-day long automated test and stored the generated unique strings in a MySQL database. During this test period, I used 5 different lengths (5, 10, 15, 20, 50) and +/-0.5 million records were inserted for each length. During my test, only the length 5 generated +/-3K duplicates out of 0.5 million and the remaining lengths didn't generate any duplicates. So we can say that if we use a length of 15 or above with Scott's functions, then we can generate highly reliable unique strings. Here is the table showing my research data: UpdateI created a simple Heroku app using these functions that returns the token as a JSON response. The app can be accessed at https://uniquestrings.herokuapp.com/api/token?length=15
halfer 19.6k17 gold badges92 silver badges175 bronze badges answered Jun 5, 2017 at 9:14
RehmatRehmat 4,3773 gold badges21 silver badges37 bronze badges 3 You can use UUID(Universally Unique Identifier), it can be used for any purpose, from user authentication string to payment transaction id. A UUID is a 16-octet (128-bit) number. In its canonical form, a UUID is represented by 32 hexadecimal digits, displayed in five groups separated by hyphens, in the form 8-4-4-4-12 for a total of 36 characters (32 alphanumeric characters and four hyphens).
//calling funtion
some example outputs will be like:
hope it helps someone in future :) answered Aug 11, 2016 at 13:38
DeveloperDeveloper 3,6574 gold badges36 silver badges45 bronze badges 1 This function will generate a random key using numbers and letters:
Example output:
answered Sep 24, 2012 at 18:16
2 I use this one-liner:
where length is the length of the desired string (divisible by 4, otherwise it gets rounded down to the nearest number divisible by 4) answered Oct 12, 2014 at 3:07
DudeOnRockDudeOnRock 3,4663 gold badges25 silver badges57 bronze badges 2 Use the code below to generate the random number of 11 characters or change the number as per your requirement.
or we can use custom function to generate the random number
Sahil 1,33513 silver badges33 bronze badges answered Dec 5, 2016 at 12:54
3
e.g (in pseudo code)
answered Dec 4, 2009 at 10:52
1 For really random strings, you can use
outputs :
so even if you try to generate string multiple times at exact same time, you will get different output. answered May 11, 2018 at 13:28 This is a simple function that allows you to generate random strings containing Letters and Numbers (alphanumeric). You can also limit the string length. These random strings can be used for various purposes, including: Referral Code, Promotional Code, Coupon Code. Function relies on following PHP functions: base_convert, sha1, uniqid, mt_rand
answered Jul 23, 2018 at 11:44
Arpit J.Arpit J. 1,09112 silver badges19 bronze badges When trying to generate a random password you are trying to :
Now, there are multiple way to generate random bytes in php like :
Then you want to turn these random bytes into a printable string : You can use bin2hex :
or base64_encode :
However, note that you do not control the length of the string if you use base64. You can use bin2hex to do that, using 32 bytes will turn into a 64 char string. But it will only work like so in an EVEN string. So basically you can just do :
answered Apr 12, 2019 at 9:04
Dylan KASDylan KAS 3,6952 gold badges12 silver badges30 bronze badges Here is what I use:
answered May 27, 2015 at 16:29
1 This people choking on a glass of water...
Simple. The posibility to this random string repeat is 0,000000000000000000000000000001^70 answered Jun 27, 2021 at 0:02
2 Here is ultimate unique id generator for you. made by me.
you can echo any 'var' for your id as you like. but $mdun is better, you can replace md5 to sha1 for better code but that will be very long which may you dont need. Thank you. answered Jul 17, 2013 at 23:08
2 I like to use hash keys when dealing verification links. I would recommend using the microtime and hashing that using MD5 since there should be no reason why the keys should be the same since it hashes based off of the microtime.
René Vogt 42k14 gold badges74 silver badges95 bronze badges answered Mar 7, 2016 at 4:58
answered Jan 8, 2014 at 12:20
NidhinNidhin 1,79821 silver badges22 bronze badges If you want to generate a unique string in PHP, try following.
In this,
answered Dec 6, 2018 at 8:24
akshaypjoshiakshaypjoshi 1,2181 gold badge13 silver badges23 bronze badges Simple 'one line' string hash generator like
answered Jul 26, 2021 at 6:59
after reading previous examples I came up with this:
I duplicate 10 times the array[0-9,A-Z] and shuffle the elements, after I get a random start point for substr() to be more 'creative' :) you can add [a-z] and other elements to array, duplicate more or less, be more creative than me answered Feb 25, 2015 at 16:09
above function will generate you a random string which is length of 11 characters. answered May 20, 2017 at 11:21
I always use this my function to generate a custom random alphanumeric string... Hope this help.
It generates for example: Y1FypdjVbFCFK6Gh9FDJpe6dciwJEfV6MQGpJqAfuijaYSZ86g If you want compare with another string to be sure that is a unique sequence you can use this trick...
it generate two unique strings: qsBDs4JOoVRfFxyLAOGECYIsWvpcpMzAO9pypwxsqPKeAmYLOi Ti3kE1WfGgTNxQVXtbNNbhhvvapnaUfGMVJecHkUjHbuCb85pF Hope this is what you are looking for... answered Mar 9, 2019 at 18:58
AlessandroAlessandro 8429 silver badges23 bronze badges A simple solution is to convert base 64 to alphanumeric by discarding the non-alphanumeric characters. This one uses random_bytes() for a cryptographically secure result.
Edit: I just revisited this because I need something a bit more flexible. Here is a solution that performs a bit better than the above and gives the option to specify any subset of the ASCII character set:
answered Feb 8, 2020 at 23:50
Jon HulkaJon Hulka 1,21910 silver badges15 bronze badges one can use this code. i tested with 35,000,00 IDs no duplicates.
you are free to modify it as your need. and if you have any suggestions feel free to comment.it is recommended that you should check every id in your database before using this ids by doing that you have 100% unique ids in your database. answered Nov 10, 2021 at 8:59
Somen DasSomen Das 1411 silver badge6 bronze badges 1 Scott, yes you are very write and good solution! Thanks. I am also required to generate unique API token for my each user. Following is my approach, i used user information (Userid and Username):
Please have a look and let me know if any improvement i can do. Thanks answered Mar 25, 2014 at 15:59
Here is what I'm using on one of my projects, it's working great and it generates a UNIQUE RANDOM TOKEN:
Please note that I multiplied the timestamp by three to create a confusion for whoever user might be wondering how this token is generated ;) I hope it helps :) answered Aug 1, 2015 at 9:41
Fery KaszoniFery Kaszoni 3,9001 gold badge17 silver badges11 bronze badges 2 I think this is the best method to use.
answered Apr 18, 2017 at 6:14
we can use this two line of code to generate unique string have tested around 10000000 times of iteration
answered May 3, 2018 at 9:07
Here is a fun one
Stolen from laravel here answered May 16, 2021 at 17:08
OUTPUT
answered Aug 12 at 10:14
Ram PukarRam Pukar 1,45313 silver badges16 bronze badges Not the answer you're looking for? Browse other questions tagged php string random uniqueidentifier or ask your own question.How do you generate random alphanumeric strings?Method 1: Using Math.random() Here the function getAlphaNumericString(n) generates a random number of length a string. ... . Method 3: Using Regular Expressions. First take char between 0 to 256. ... . Method 4: Generating random String of UpperCaseLetter/LowerCaseLetter/Numbers.. How do you generate random unique alphanumeric strings in laravel?If you need to generate unique random string then you can use str_random() helper of Laravel. It is very simple and you can use easily. you can easily generate random string in laravel 6, laravel 7, laravel 8 and laravel 9 version using str helper.
How do you generate unique random strings?There are many ways to generate a random, unique, alphanumeric string in PHP which are given below: 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.
How do I randomize a string in PHP?The str_shuffle() function randomly shuffles all the characters of a string.
|