Last active
December 29, 2017 01:55
-
-
Save matstani/bca8f605d2ef679ce3c6f9f2294e5ed7 to your computer and use it in GitHub Desktop.
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 | |
define('PASSWORD_LENGTH', 6); | |
function randGet($chars) { | |
$n = rand(0, strlen($chars) - 1); | |
return $chars[$n]; | |
} | |
function genpwd($length) { | |
$alpha = 'abcdefghijklmnopqrstuwxyz'; | |
$digit = '0123456789'; | |
$pwd = array(); | |
// アルファベットと数字をそれぞれ最低1文字含む | |
$pwd[] = randGet($alpha); | |
$pwd[] = randGet($digit); | |
$num_rest = $length - count($pwd); | |
for($i = 0; $i < $num_rest; $i++) { | |
$pwd[] = randGet($alpha . $digit); | |
} | |
shuffle($pwd); | |
return implode($pwd); | |
} | |
echo genpwd(PASSWORD_LENGTH)."\n"; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment