Skip to content

Instantly share code, notes, and snippets.

@rsky
Created January 23, 2011 23:07
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rsky/792558 to your computer and use it in GitHub Desktop.
Save rsky/792558 to your computer and use it in GitHub Desktop.
PHPのコマンドラインツールでパスワードを要求するときの定番コード
<?php
// STDOUT or STDERR は場合に応じて
fwrite(STDERR, 'Password: ');
if (strncasecmp(PHP_OS, 'WIN', 3) === 0) {
// WindowsではエコーバックをOFFにできない?
@flock(STDIN, LOCK_EX);
$password = fgets(STDIN);
@flock(STDIN, LOCK_UN);
} else {
system('stty -echo'); // エコーバックをOFFにする
@flock(STDIN, LOCK_EX);
$password = fgets(STDIN);
@flock(STDIN, LOCK_UN);
system('stty echo'); // エコーバックをONに戻す
}
fwrite(STDERR, "\n");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment