88

I want to be able to let a PHP program wait for user's input. For example:

  1. Script requests authorization code from server (this code will be sent via e-mail)
  2. Script waits for user to enter authorization code
  3. Script continues

How should I do step 2? (Is it possible?)

  • 2
    How about read the man page on php cli usage, particularly useful would be this comment -> http://www.php.net/manual/en/features.commandline.php#94924 – Crisp Mar 10 '13 at 12:50
  • Thanks @Crisp, you can make that an answer. –  Mar 10 '13 at 12:51
  • I don't think this is a duplicate of the answer linked. You could use the code provided by Crisp which involves file handles. Or simply use the readline() function which will prompt for user input: http://www.php.net/manual/en/function.readline.php assuming your PHP was compiled with readline support. – Gabe Apr 02 '14 at 09:49
  • @Gabe the question is maybe not the same, but an answer to this question was given on the other question, which makes closing fair. –  Apr 02 '14 at 09:51
  • You might use the built-in php function: "readline()", usage: $line = readline("Command: "); – Mohammad Anini Dec 16 '14 at 18:49

1 Answers1

210

The php man page for the cli has a comment here detailing a solution, (copied here for anyone else looking)

<?php
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;
}
fclose($handle);
echo "\n"; 
echo "Thank you, continuing...\n";
?>
Toby Allen
  • 10,997
  • 11
  • 73
  • 124
Crisp
  • 11,417
  • 3
  • 38
  • 41
  • Can this be doing loop, that terminates on input? – azurinko Sep 26 '17 at 14:06
  • @azurinko yes https://gist.github.com/jordan314/f633a58c68a7d923139b891065365133 – alana314 Nov 28 '17 at 01:24
  • 6
    might as well just do `$line = trim(fgets($handle))` since 99.9% of the time you wouldn't want the newline in the captured input – But those new buttons though.. Mar 30 '18 at 18:35
  • 3
    You can use additional check ```if( php_sapi_name() === 'cli' ) { } ``` to determine if the current invocation is from CLI https://stackoverflow.com/questions/933367/php-how-to-best-determine-if-the-current-invocation-is-from-cli-or-web-server – Arnis Juraga Jan 21 '19 at 09:22
  • @billynoah but you might want leading / trailing spaces, so maybe `$line = trim(fgets($handle), "\n\r");`? – spiral_generator Feb 11 '20 at 17:13
  • @spiral_generator, anything's possible but I'd be hard pressed to think of a situation where leading/trailing spaces from user input are ever something I want. – But those new buttons though.. Feb 11 '20 at 17:55
  • Could someone explain why you cannot use `readline()` for this? I'm new to using PHP for cli scripts. https://www.php.net/manual/en/function.readline.php – Hugo Nov 25 '20 at 09:08
  • 2
    @Hugo: You *should* use `readline()` for this. This is unnecessarily complicated. – rinogo Nov 22 '21 at 15:52