Just a quick tip, since I spent a good hour figuring this out recently. PHP has no native way of doing an interactive password prompt, when running as CLI. You can however use bash for the task. Of course this means that it won’t work on Windows, but you should be fine on most anything else. Edit: Using Windows Scripting Host, it’s possible to pop a prompt up for the user to type in. I’ve extended the example to use this technique for Windows based systems.
/**
* Interactively prompts for input without echoing to the terminal.
* Requires a bash shell or Windows and won't work with
* safe_mode settings (Uses `shell_exec`)
*/
function prompt_silent($prompt = "Enter Password:") {
if (preg_match('/^win/i', PHP_OS)) {
$vbscript = sys_get_temp_dir() . 'prompt_password.vbs';
file_put_contents(
$vbscript, 'wscript.echo(InputBox("'
. addslashes($prompt)
. '", "", "password here"))');
$command = "cscript //nologo " . escapeshellarg($vbscript);
$password = rtrim(shell_exec($command));
unlink($vbscript);
return $password;
} else {
$command = "/usr/bin/env bash -c 'echo OK'";
if (rtrim(shell_exec($command)) !== 'OK') {
trigger_error("Can't invoke bash");
return;
}
$command = "/usr/bin/env bash -c 'read -s -p \""
. addslashes($prompt)
. "\" mypassword && echo \$mypassword'";
$password = rtrim(shell_exec($command));
echo "\n";
return $password;
}
}
Related posts:
- How to Use PHP Namespaces, Part 2: Importing, Aliases, and Name Resolution In the second part of Craig's PHP namespaces series, he...
- How to Use PHP Namespaces, Part 1: The Basics In the first part of a series of articles, Craig...
- How to Use PHP Namespaces, Part 3: Keywords and Autoloading In the final part of his series explaining PHP namespaces,...
- How to Install MySQL Installing MySQL is easier than you think. Craig provides a...
- How to Install PHP 5.3 on Windows PHP 5.3 is the most significant update since version 5.0....







Will it work on Windows with Cygwin Tools installed? That’s almost a bash, right?
May 1st, 2009 at 7:14 am
We used php-gtk for this. Works great on windows!
May 1st, 2009 at 7:31 am
How about readline?
http://www.php.net/manual/en/function.readline.php
May 1st, 2009 at 10:26 am
@magnusdopus: Good tip. The only downside is that it adds a pretty heavy dependency if you aren’t already using gtk for anything else. I was thinking that it must be possible to use windows scripting host to pop up a modal box somehow, but I haven’t investigated further.
@Coder Jim: In theory, but Cygwin has some problems with interactive shell features, so I wouldn’t be surprised if it didn’t work. Haven’t tried it though.
@Lachlan: readline echoes input to the screen.
May 1st, 2009 at 5:42 pm
something like this could work at Win.
prompt.vbs:
pass = InputBox("Input password", "", "password here") wscript.echo passprompt.php:
echo (exec('cscript //nologo prompt.vbs'));If promp.vbs and prompt.php are in same folder and php is Path, cmd could be e.g.:
D:\>php prompt.php
May 1st, 2009 at 6:41 pm
Of course, in example above, vbs script should be in two lines.
Or in one:
wscript.echo InputBox("Input password", "", "password here")May 1st, 2009 at 7:00 pm
@ognjen: Yeah, Wordpress messes peoples comments up. It’s rather annoying.
I just tested it on Windows and it works. It would be nice to do without the vbs file, but I guess the php-script could just generate it as a temp-file, and delete it afterwards.
The InputBox doesn’t mask the password, but it seems to be possible to do, using a few more lines of vb-script:
Wscript.Echo(CreateObject("ScriptPW.Password").GetPassword())May 1st, 2009 at 7:10 pm
OK, I couldn’t get the
ScriptPWtrick to work, but I’ve now updated the snippet above to use the basic password-prompt, if the OS is Windows. This should make the function fully portable.May 1st, 2009 at 7:54 pm
Not bad at all – i’ve just been using javascript based techniques. This looks very useful also php-gtk looks good also. Thanks!
May 5th, 2009 at 5:53 pm
Nice code.
May 13th, 2009 at 8:13 pm
This works great. Thanks!
May 16th, 2009 at 3:23 am