Created
September 25, 2023 15:19
-
-
Save matteocaberlotto/93ef2bf5f210417be118feaa88d96f06 to your computer and use it in GitHub Desktop.
Password generator PHP
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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