Hiding data input PHP-cli Linux

Question:

There is a cli script in PHP. There is data that is entered from the keyboard, it would be desirable to hide it, for example, to close it with asterisks. I am currently using the readline function:

$password = readline('Password: ');

In bash, this is done with the read command:

read -s var

Is it possible to implement this using php?

Answer:

You can proceed as follows

<?php
echo 'Password: ';
$oldStyle = shell_exec('stty -g');
shell_exec('stty -echo');
$password = rtrim(fgets(STDIN), "\n");
shell_exec('stty ' . $oldStyle);
echo "\r\n";
echo $password;
echo "\r\n";
Scroll to Top