Command Line Password Prompt in PHP

后端 未结 10 1525
北海茫月
北海茫月 2020-11-30 21:48

I\'m writing a command line tool to help my web app. It needs a password to connect to the service. I\'d like the script to show a password prompt so I don\'t have to pass i

10条回答
  •  旧巷少年郎
    2020-11-30 22:21

    Theorically you can do it using stream_set_blocking(), but looks like there are some PHP bugs managing STDIN.

    Look: http://bugs.php.net/bug.php?id=34972 http://bugs.php.net/bug.php?id=36030

    Try yourself:

    echo "Enter Password: ";
    $stdin = fopen('php://stdin','r');
    // Trying to disable stream blocking
    stream_set_blocking($stdin, FALSE) or die ('Failed to disable stdin blocking');
    // Trying to set stream timeout to 1sec
    stream_set_timeout ($stdin, 1) or die ('Failed to enable stdin timeout');

提交回复
热议问题