Home > Backend Development > PHP Tutorial > shell - php的cli如何实现读取用户输入,但隐藏这段字符输出

shell - php的cli如何实现读取用户输入,但隐藏这段字符输出

WBOY
Release: 2016-06-06 20:41:37
Original
1029 people have browsed it

简单来说就是比如大家用ssh登陆时,一般要求输入密码,然后当你输入时你的输入是被隐藏的,但是后端实际上获取到了你的输入,这是怎么做到的呢?

回复内容:

简单来说就是比如大家用ssh登陆时,一般要求输入密码,然后当你输入时你的输入是被隐藏的,但是后端实际上获取到了你的输入,这是怎么做到的呢?

<code>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;
  }
}

echo prompt_silent();
</code>
Copy after login

参考:http://www.sitepoint.com/interactive-cli-password-prompt-in-php/

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template