In the above example, I once chose a length of 10 characters. Uses wprand() to create passwords with far less predictability than similar native PHP functions like rand() or mtrand(). If you replace the 10 with a different number, you can shorten the number of characters. If this should be too long for your purpose, you can also directly limit the string with substr(): echo substr(md5(rand()),0,10) The function rand() creates a random number and md5() makes an MD5 hash from that, which will look similiar to the second line above. If you need to use this in a PHP 5 project, feel free to grab a copy of random_compat, which is a polyfill for these functions.The easiest way to achieve this, is as follows with a combination of md5() and rand(): echo md5(rand()) Throw new Exception('$keyspace must be at least two characters long') $keyspace = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ' * string $keyspace A string of all possible characters * int $length How many characters do we want? * For PHP 7, random_int is a PHP core function * pseudorandom number generator (random_int) Random Functions Change language: Submit a Pull Request Report a Bug rand (PHP 4, PHP 5, PHP 7, PHP 8) rand Generate a random integer Description rand (): int rand ( int min, int max ): int If called without the optional min, max arguments rand () returns a pseudo-random integer between 0 and getrandmax (). * Generate a random string, using a cryptographically secure This can cause trouble in several common scenarios, such as storing this. It's trivial to turn the latter function into a string generator function:
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |