Skip to content

Instantly share code, notes, and snippets.

@lasekmiroslaw
Last active July 24, 2018 19:59
Show Gist options
  • Save lasekmiroslaw/4893f257b4b229323a9bab431cef342a to your computer and use it in GitHub Desktop.
Save lasekmiroslaw/4893f257b4b229323a9bab431cef342a to your computer and use it in GitHub Desktop.
Random token generator
class TokenGenerator
{
private const ALPHABET = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
public function getRandomSecureToken(int $length): string
{
$maxNumber = strlen(self::ALPHABET);
$token = '';
for ($i = 0; $i < $length; $i++) {
$token .= self::ALPHABET[random_int(0, $maxNumber - 1)];
}
return $token;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment