19
18

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

PHPで英数記号8ケタパスワードを生成する

Posted at

#PHPでパスワード用文字列を作る
Drupal開発で別途パスワードを自動的に生成する必要があったのでメモ。

MYMODULE.module
/*
 *
 * 英数小文字8ケタのパスワードを生成する
 * @params $length: 
 */
function create_passwd( $length = 8 ){
    //vars
    $pwd = array();
    $pwd_strings = array(
        "sletter" => range('a', 'z'),
        "cletter" => range('A', 'Z'),
        "number"  => range('0', '9'),
        "symbol"  => array_merge(range('!', '/'), range(':', '?'), range('{', '~')),
    );

    //logic
    while (count($pwd) < $length) {
        // 4種類必ず入れる
        if (count($pwd) < 4) {
            $key = key($pwd_strings);
            next($pwd_strings);
        } else {
        // 後はランダムに取得
            $key = array_rand($pwd_strings);
        }
        $pwd[] = $pwd_strings[$key][array_rand($pwd_strings[$key])];
    }
    // 生成したパスワードの順番をランダムに並び替え
    shuffle($pwd);

    return implode($pwd);
}

$pwd_strings部分を調整して、「英数小文字のみ」とかにできる。

19
18
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
19
18

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?