PHP中的命令行密码提示 [英] Command Line Password Prompt in PHP
问题描述
我正在编写一个命令行工具来帮助我的Web应用程序.需要密码才能连接到服务.我希望脚本显示密码提示,因此我不必将其作为命令行参数传递.
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 it as a command line argument.
这很容易,但是我希望它在输入时不将密码回显到屏幕上.如何使用PHP做到这一点?
That's easy enough, but I'd like it to not echo the password to the screen as it's typed. How can I do this with PHP?
使用纯PHP(没有system('stty')
)并用*
替换字符的好处.
Bonus points for doing it in pure PHP (no system('stty')
) and replacing the characters with *
.
该脚本将在类似Unix的系统(Linux或Mac)上运行.该脚本是用PHP编写的,很可能会一直保持这种状态.
The script will run on a unix like system (linux or mac). The script is written in PHP, and will most likely stay like that.
另外,stty
的记录方式为:
echo "Password: ";
system('stty -echo');
$password = trim(fgets(STDIN));
system('stty echo');
// add a new line since the users CR didn't echo
echo "\n";
我不想在那里没有system()
呼叫.
I'd prefer to not have the system()
calls in there.