Password prompt for PHP CLI

January 28th, 2012 Leave a comment Go to comments

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";

 

If you would like a reply to your comment, you must leave your email address! We receive dozens of questions every month from people who don't leave us with any way to contact them, so we have no choice but to ignore the question. We try to reply to as many questions as we can, if we know the email address of the person who asked the question. Thanks in advance for writing in :)

Allowed HTML: <b>, <i>, <em>, <strong>. All other < and > will be replaced with &lt; and &gt;.