21 lines
552 B
PHP
21 lines
552 B
PHP
<?php
|
|
|
|
/**
|
|
* Create cryptographically secure tokens of custom length
|
|
*
|
|
* @author Scott https://stackoverflow.com/questions/1846202/php-how-to-generate-a-random-unique-alphanumeric-string/13733588#13733588
|
|
*/
|
|
function getToken($length){
|
|
$token = "";
|
|
$codeAlphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
|
|
$codeAlphabet.= "abcdefghijklmnopqrstuvwxyz";
|
|
$codeAlphabet.= "0123456789";
|
|
$max = strlen($codeAlphabet);
|
|
|
|
for ($i=0; $i < $length; $i++) {
|
|
$token .= $codeAlphabet[random_int(0, $max-1)];
|
|
}
|
|
|
|
return $token;
|
|
}
|