Is it really not possible to write a php cli password prompt that hides the password in windows?(难道真的不能写一个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
";
?>
这篇关于难道真的不能写一个php cli密码提示在windows中隐藏密码吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:难道真的不能写一个php cli密码提示在windows中隐藏密码吗?
基础教程推荐
- 在 PHP 中强制下载文件 - 在 Joomla 框架内 2022-01-01
- 在 Woocommerce 中根据运输方式和付款方式添加费用 2021-01-01
- 在多维数组中查找最大值 2021-01-01
- mysqli_insert_id 是否有可能在高流量应用程序中返回 2021-01-01
- XAMPP 服务器不加载 CSS 文件 2022-01-01
- 超薄框架REST服务两次获得输出 2022-01-01
- 通过 PHP SoapClient 请求发送原始 XML 2021-01-01
- 如何在 PHP 中的请求之间持久化对象 2022-01-01
- Libpuzzle 索引数百万张图片? 2022-01-01
- WooCommerce 中选定产品类别的自定义产品价格后缀 2021-01-01