1stPHP

ランダムパスワード

ソース

<?php

$mode   $_POST["mode"];
$length $_POST["length"];

//文字数を1〜99の間にします
if (!$length || ($length || $length 99)){
  
$length  8;
}

//フォームの表示
print '
  <form action="'
.$_SERVER['PHP_SELF'].'" method="post">
  <p>
    ランダムな 
    <input type="text" name="length" size="4" value="'
.$length.'" />
    文字で
    <input type="hidden" name="mode" value="create" />
    <input type="submit" name="submit" value="作成" />
  </p>
  </form>
'
;


if (
$mode == "create") {
  
  
//パスワードに使用する文字列
  
$str "abcdefghkmnpqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ2345679";
  
  
//文字列を文字要素に分割し配列にセットします。
  
$ary  preg_split("//"$str, -1PREG_SPLIT_NO_EMPTY);
  
  
$password "";
  for(
$i=0$i<$length$i++ ) {
    
//array_randで得たランダムなキーを使い、$aryから1文字ずつ$passwordにセットします。
    //srand()やmt_srand()は自動的にされています。
    
$key array_rand($ary1);
    
$password .= $ary[$key];
  }
  
  print 
"<p>$password</p>";
  
}

?>

サンプル実行

ランダムパスワード