Hide password input in CLI PHP script?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • emerth

    Hide password input in CLI PHP script?

    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

    #2
    Re: Hide password input in CLI PHP script?

    You can call "stty -echo" to disable echo'ing on the terminal. Here's
    a script to do it:



    On Jul 12, 6:56 pm, emerth <eme...@hotmail .comwrote:
    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!

    Comment

    • Toby A Inkster

      #3
      Re: Hide password input in CLI PHP script?

      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

      Comment

      Working...