Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Hide password input in CLI PHP script?

1 view
Skip to first unread message

emerth

unread,
Jul 12, 2007, 6:56:08 PM7/12/07
to
Hi,

I have a command line PHP script that logs into a network service with
a user name specified on the command line. The script then prompts for
a password.

Currently the password is echoed on the screen/terminal as it is
typed.

Is it possible to stop the password being echoed on the command line
while it is typed? Or perhaps just print asterix's instead of the
literal characters?

A few details: platform is Linux, PHP version is 4.3.9.

Cheers!

petersprc

unread,
Jul 12, 2007, 8:43:37 PM7/12/07
to
You can call "stty -echo" to disable echo'ing on the terminal. Here's
a script to do it:

http://marc.info/?l=php-dev&m=117442143122154

Toby A Inkster

unread,
Jul 13, 2007, 8:04:30 AM7/13/07
to
petersprc wrote:

> You can call "stty -echo" to disable echo'ing on the terminal.

Indeed. As an example, the following command ought to prompt for a
password and save it into a file called "foo" without echoing it to the
command line:

stty -echo; echo -n 'Password:'; head -n1 >foo; echo ''

with a bit of imagination, it should be pretty easy to see how you can
work this into a PHP program:

<?php
echo 'Password: ';
$pwd = preg_replace('/\r?\n$/', '', `stty -echo; head -n1`);
echo "\n";
echo "Your password was: {$pwd}.\n";
?>

--
Toby A Inkster BSc (Hons) ARCS
[Geek of HTML/SQL/Perl/PHP/Python/Apache/Linux]
[OS: Linux 2.6.12-12mdksmp, up 22 days, 15:34.]

demiblog 0.2.0 Released
http://tobyinkster.co.uk/blog/2007/06/28/demiblog-0.2.0/

0 new messages