Password prompt for PHP CLI

January 28, 2012
Do you use PHP from the command line and need a password prompt? This is how you do. Windows:
$scriptpw = new Com('ScriptPW.Password');
$password = $scriptpw->getPassword();
The Windows version requires the file scriptpw.dll. This comes with some versions of Windows, such as XP, but not others, such as 2000, Vista, or 7. If you don't have the file, simply copy scriptpw.dll from the system32 folder of another machine to the system32 folder of your machine. Then, from a Command Prompt window (running as Administrator for Vista and 7) enter the command regsvr32 scriptpw.dll Linux:
system('stty -echo');
$password = trim(fgets(STDIN));
system('stty echo');
echo "\n";