Skip to content

Instantly share code, notes, and snippets.

@dsturm
Last active April 26, 2017 09:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dsturm/6eae39851bde9eb4923ed5d9a6d9f5e5 to your computer and use it in GitHub Desktop.
Save dsturm/6eae39851bde9eb4923ed5d9a6d9f5e5 to your computer and use it in GitHub Desktop.
Create Auth Tokens with PHP
function create_auth_token($length = 64)
{
if (!$length || intval($length) <= 8) {
$length = 64;
}
$bytes = null;
if (function_exists('random_bytes')) {
$bytes = random_bytes($length);
} elseif (function_exists('mcrypt_create_iv')) {
$bytes = mcrypt_create_iv($length, MCRYPT_DEV_URANDOM);
} elseif (function_exists('openssl_random_pseudo_bytes')) {
$bytes = openssl_random_pseudo_bytes($length);
}
return $bytes ? bin2hex($bytes) : false;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment