Interactive CLI promt with PHP without echoing to terminal


/ Published in: Other
Save to your folder(s)



Copy this code and paste it in your HTML
  1. /**
  2. * Interactively prompts for input without echoing to the terminal.
  3. * Requires a bash shell or Windows and won't work with
  4. * safe_mode settings (Uses `shell_exec`)
  5. */
  6. function prompt_silent($prompt = "Enter Password:") {
  7. if (preg_match('/^win/i', PHP_OS)) {
  8. $vbscript = sys_get_temp_dir() . 'prompt_password.vbs';
  9. file_put_contents(
  10. $vbscript, 'wscript.echo(InputBox("'
  11. . addslashes($prompt)
  12. . '", "", "password here"))');
  13. $command = "cscript //nologo " . escapeshellarg($vbscript);
  14. $password = rtrim(shell_exec($command));
  15. unlink($vbscript);
  16. return $password;
  17. } else {
  18. $command = "/usr/bin/env bash -c 'echo OK'";
  19. if (rtrim(shell_exec($command)) !== 'OK') {
  20. trigger_error("Can't invoke bash");
  21. return;
  22. }
  23. $command = "/usr/bin/env bash -c 'read -s -p ""
  24. . addslashes($prompt)
  25. . "" mypassword && echo $mypassword'";
  26. $password = rtrim(shell_exec($command));
  27. echo "n";
  28. return $password;
  29. }
  30. }
  31.  

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.