Skip to content

Instantly share code, notes, and snippets.

@matteocaberlotto
Created September 25, 2023 15:19
Show Gist options
  • Save matteocaberlotto/93ef2bf5f210417be118feaa88d96f06 to your computer and use it in GitHub Desktop.
Save matteocaberlotto/93ef2bf5f210417be118feaa88d96f06 to your computer and use it in GitHub Desktop.
Password generator PHP
<?php
$length = 32;
if ($argc > 1) {
$length = intval($argv[1]);
}
$token = '';
$rand_length = 4;
if ($argc > 2) {
$rand_length = 3;
}
for ($i=0; $i < $length; $i++) {
$type = mt_rand(1, $rand_length);
switch($type) {
case '1':
// cifre 0-9
$rand = mt_rand(48, 57);
$token .= chr($rand);
break;
case '2':
// lettere A-Z
$rand = mt_rand(65, 90);
$token .= chr($rand);
break;
case '3':
// lettere a-z
$rand = mt_rand(97, 122);
$token .= chr($rand);
break;
case '4':
// caratteri random
$chars = array('!','%','&','=','?',']','[');
$token .= $chars[mt_rand(0, count($chars) - 1)];
break;
}
}
echo $token . "\n";
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment