难道真的不能写一个php cli密码提示在windows中隐藏密码吗?
我花了几个小时试图找到一种在 php 中编写跨平台密码提示的方法,以隐藏用户输入的密码.虽然这在 Unix 环境中通过使用 stty -echo 很容易实现,但我尝试了各种 passthru() 和 system() 调用方法来使 Windows 做同样的事情,但无济于事.
I have spent several hours trying to find a means of writing a cross platform password prompt in php that hides the password that is input by the user. While this is easily accomplished in Unix environments through the use of stty -echo, I have tried various means of passthru() and system() calls to make windows do the same thing to no avail.
我试过了:
passthru('set /p pass=Password: ');
system('echo %pass% > out.txt');
$pass = file_get_contents('out.txt', 'r');
这似乎挂在 passthru('set/p pass=Password: ');不允许我输入任何文本的行,必须用 Ctrl-c 杀死.
This seems to hang on the passthru('set /p pass=Password: '); line without allowing me to input any text and must be killed with a Ctrl-c.
我还尝试了各种 fgetc 和 fgets 方法以及打印退格字符来隐藏输入,因为这适用于其他语言.但是,PHP 似乎无法在回车之前与文本交互.
I have also tried various methods of fgetc and fgets and printing backspace characters to hide the input since this works in other languages. However, PHP does not appear to be able to interact with text prior to a carriage return.
我真的很想找到一种方法来完成这项工作,这是一项不可能完成的任务还是可以完成的事情?
I would really like to find a way to make this work, is this an impossible task or is this something that can be done?
请注意,我知道我可以将 php 脚本包装在一个批处理文件中并将密码作为命令行参数传递,但在这种情况下这对我不起作用.
Note that I am aware that I could wrap the php script in a batch file and pass the password as a command line argument, but that doesn't work for me in this case.
推荐答案
这是一个 Windows 解决方案,使用 PHP 的 COM 扩展.我在 Windows XP 上使用 PHP 5.2.6 对此进行了测试.
Here's a Windows solution, using the COM extension for PHP. I tested this on Windows XP with PHP 5.2.6.
<?php
$pwObj = new Com('ScriptPW.Password');
print "Password: ";
$passwd = $pwObj->getPassword();
echo "Your password is $passwd
";
?>
相关文章