Skip to content

Instantly share code, notes, and snippets.

@sectsect
Created February 9, 2018 16:59
Show Gist options
  • Save sectsect/849a821ad4dc9024f132de7317506429 to your computer and use it in GitHub Desktop.
Save sectsect/849a821ad4dc9024f132de7317506429 to your computer and use it in GitHub Desktop.
Create Ramdom Tokens with PHP
<?PHP
function get_token($length = 32) {
if ( ! isset($length) || intval($length) <= 8 ) {
$length = 32;
}
// PHP 7.0+
if ( function_exists('random_bytes') ) {
return bin2hex(random_bytes($length));
}
// PHP 5.3+
if ( function_exists('openssl_random_pseudo_bytes') ) {
return bin2hex(openssl_random_pseudo_bytes($length));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment