Skip to content

Instantly share code, notes, and snippets.

@mikemix
Last active December 10, 2015 23:58
Show Gist options
  • Save mikemix/4512886 to your computer and use it in GitHub Desktop.
Save mikemix/4512886 to your computer and use it in GitHub Desktop.
Create random string (salt)
<?php
/**
* Create random string.
*
* @param int $length
* @return string
*/
function getRandomSalt($length=8)
{
// char list
$symbols=array_merge(
range(0,9), range('a','z'), range('A','Z'),
array(
'!','@','#','$','%','^','&','*','(',')','{','}','<',
'>','/','?',';','_',':','=','+','~','[',']','|','`',
)
);
// random sort
shuffle($symbols);
// cut first N elements
$randomSymbols=array_slice($symbols, 0, $length);
// return the salt
return implode('', $randomSymbols);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment