Skip to content
Advertisement

PHP: How to use STDIN to get secret input for passwords on Linux based systems

What is the most up-to-date way to get input from STDIN but not see what was typed in PHP CLI mode, so things like passwords are not shown?

Difference being, only on Linux based systems like Ubuntu, most other answers seem to be based on Windows support, it is not clear which is the best way for Linux?

Advertisement

Answer

You could do that if terminal has stty available

<?php

shell_exec('stty -echo');

echo 'Enter pw: ';
$value = fgets(STDIN, 4096);

shell_exec('stty echo');

var_dump($value);

If you need the fancy asterisk you could use systemd-ask-password if that’s available

<?php

$value = shell_exec('systemd-ask-password "Enter pw:"');

var_dump($value);

Read more here: https://learnfromnoobs.com/hide-user-input-password/

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement