Skip to content

Instantly share code, notes, and snippets.

@nicklasos
Last active August 29, 2015 14:05
Show Gist options
  • Save nicklasos/06dc9795d8f3c8663d51 to your computer and use it in GitHub Desktop.
Save nicklasos/06dc9795d8f3c8663d51 to your computer and use it in GitHub Desktop.
Prompt password
<?php
/**
* 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`)
*
* @see http://us.php.net/manual/en/function.ncurses-noecho.php
* @see http://us.php.net/manual/en/function.ncurses-getch.php
*
* Works only on normal OS, not in windows.
*/
function prompt_silent($prompt = "Enter Password: ")
{
$command = "/usr/bin/env bash -c 'echo OK'";
if (rtrim(shell_exec($command)) !== 'OK') {
trigger_error("Can't invoke bash");
return null;
}
$command =
"/usr/bin/env bash -c 'read -s -p \"" .
addslashes($prompt) .
"\" mypassword && echo \$mypassword'";
$password = rtrim(shell_exec($command));
return $password;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment