开发者

PHP command-line: How do I read in a typed-in password without displaying the typed chars on the console?

开发者 https://www.devze.com 2022-12-09 02:19 出处:网络
I want to read in a password as part of a command-line script, but do not what the typed password to appear on the screen. This seems to be a fairly common feature in command-line apps, but my Google-

I want to read in a password as part of a command-line script, but do not what the typed password to appear on the screen. This seems to be a fairly common feature in command-line apps, but my Google-fu has failed in identifying how this works. Is it a feature of the OS? Do you have to intercept the input stream somehow so it doesn't write out to the console? I would like to use this in a php command line script, but I cannot use what I cannot understand.

Some independent searching yielded this link, where way down at the bottom they mention that mysqladmin does not use STDIN to read开发者_开发问答 in the password. How do they do it then, and how could I implement this functionality?


Answering in case someone finds this googling.

It isn't possible in pure PHP. The solution is to execute a small shell script for Linux or a small program for Windows.

The shell script would look like this for the zsh shell:

/usr/bin/env zsh -c 'stty -echo; read -r mypassword; stty echo; echo $mypassword'

A full working implementation was written by Seldaek from Composer, which you can find at https://github.com/Seldaek/cli-prompt

0

精彩评论

暂无评论...
验证码 换一张
取 消