Skip to content

Instantly share code, notes, and snippets.

@MrXploder
Created May 22, 2018 04:08
Show Gist options
  • Save MrXploder/becf7cb1654a2957055864cd2491270e to your computer and use it in GitHub Desktop.
Save MrXploder/becf7cb1654a2957055864cd2491270e to your computer and use it in GitHub Desktop.
PHP Function to return a random alphanumeric token given the desired length
<?php
/*
* @params $length (number) -> length of the random alphanumeric token
* @author MrXploder
*/
function randomKey($length) {
$pool = array_merge(range(0,9), range('a', 'z'),range('A', 'Z'));
for($i=0; $i < $length; $i++) {
$key .= $pool[mt_rand(0, count($pool) - 1)];
}
return $key;
}
//USAGE:
//echo $token = randomKey(20);
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment