PHP中的命令行密码提示 [英] Command Line Password Prompt in PHP

查看:165
本文介绍了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.

推荐答案

这篇关于PHP中的命令行密码提示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆