Created
January 23, 2011 23:07
-
-
Save rsky/792558 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 | |
// 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