Page 1 of 1
CLI : Take "masked" input from STDIN ?
Posted: Thu Jul 14, 2005 12:38 pm
by Chris Corbyn
I want to take a password from STDIN for a command line tool I'm making.
Being a password I don't want the text to be displayed as it is typed...
Can I do that with PHP for CLI?
Example:
Code: Select all
echo "New Password: ";
$pass = trim(fgets(STDIN));
echo "Confirm Password: ";
$pass2 = trim(fgets(STDIN));
If I can have NOTHING printed at all (unix style) that'd be great, but starred out is also OK.
Cheers,
d11
Posted: Thu Jul 14, 2005 12:51 pm
by onion2k
I don't think you can do that with vanilla PHP. There might be an extension to help out though.
Or, super long shot, you might be able to set the STDIN stream as non-blocking, detect when a character comes in, then use fputs(STDOUT) to print a backspace followed by a * .. Tricky to code that though.
Posted: Thu Jul 14, 2005 12:56 pm
by Chris Corbyn
I'll give that a go... doesn't sound overly tricky if PHP has those abailites
Thanks
Posted: Thu Jul 14, 2005 1:09 pm
by anjanesh
Is there no C version of getch() ? Getting a character at a time not displaying it ?
Posted: Thu Jul 14, 2005 1:11 pm
by timvw
Here is a *nix solution

Meaby you can do the same in windows with ECHO on/off
Code: Select all
function ttyecho($on)
{
global $ttyecho;
if($on)
{
if(isset($ttyecho))
{
exec("stty $ttyecho");
}
}
else
{
$ttyecho = exec("stty -g");
exec("stty -echo");
}
}
ttyecho(false); //turn off echoing
echo "\nEnter your password: ";
$password = trim(fgets(STDIN));
echo "\nYour password: $password\n";
ttyecho(true); //turn echoing back on
Posted: Fri Jul 15, 2005 7:54 am
by Chris Corbyn
Thanks tim... works a treat. I don't need to have it windows compatible. It's a tool for setting up unix-only software anyway
