Skip to content

Instantly share code, notes, and snippets.

@renatorib
Last active August 29, 2015 14:24
Show Gist options
  • Save renatorib/982b4f4c886aa23d8f87 to your computer and use it in GitHub Desktop.
Save renatorib/982b4f4c886aa23d8f87 to your computer and use it in GitHub Desktop.
Function to Generate Token by length
function generateToken($length = 20, $chars = 'abcdefghijklmnopqrstuvxwyzABCDEFGHIJKLMNOPQRSTUVXWYZ1234567890'){
$return = '';
while($length > 0){
$return .= $tokens[rand(0, strlen($chars)-1)];
$length--;
}
return $return;
}
@renatorib
Copy link
Author

echo generateToken();
// 7EWOvJe9urFINYMbsNQZ
echo generateToken(10);
// 5L9g6RomG7
echo generateToken(5);
// i8J4p
echo "#" . generateToken(6, "1234567890abcdef");
// #ef7a43

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment