在此示例中,我将扩展Devjar的示例。 例如他的代码。 我认为最后一个代码示例最简单,最安全。
使用他的代码时:
echo "Are you sure you want to do this? Type 'yes' to continue: ";
$handle = fopen ("php://stdin","r");
$line = fgets($handle);
if(trim($line) != 'yes'){
echo "ABORTING!\n";
exit;
}
echo "\n";
echo "Thank you, continuing...\n";
?>
您应该注意stdin模式不是二进制安全的。 您应该在模式中添加“ b”并使用以下代码:
echo "Are you sure you want to do this? Type 'yes' to continue: ";
$handle = fopen ("php://stdin","rb"); //
$line = fgets($handle);
if(trim($line) != 'yes'){
echo "ABORTING!\n";
exit;
}
echo "\n";
echo "Thank you, continuing...\n";
?>
您也可以设置最大包机。 这是我的个人例子。 我建议将其用作您的代码。 还建议直接使用STDIN而不是“ [php:// stdin”。]
/* Define STDIN in case if it is not already defined by PHP for some reason */
if(!defined("STDIN")) {
define("STDIN", fopen('php://stdin','rb'))
}
echo "Hello! What is your name (enter below):\n";
$strName = fread(STDIN, 80); // Read up to 80 characters or a newline
echo 'Hello ' , $strName , "\n";
?>