Jump to content

Recommended Posts

I've been trying to pause a php script until a key is pressed by the user & I can't get it working. I've found quit a few answers online that seem to work for everyone else but not on my system.

I'm running:  

PHP Version 5.5.9-1ubuntu4.5

Below is a few of the codes I have tried but none of them accept any input & just run the script straight through to the end of the code.

<?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;
}
echo "\n"; 
echo "Thank you, continuing...\n";
?>
------------------------------------------
<?php
echo "Enter your name\n"; // Output - prompt user
$name = fgets(STDIN);     // Read the input
echo "Hello $name";       // Output - Some text   
exit(0);                  // Script ran OK
?>
-------------------------------------------------
<?php

function anykey($s='Press any key to continue...') { 
    echo "\n".$s."\n"; 
    fgetc(STDIN); 
}  

anykey;
echo "Help";
?>
-------------------------------------------------------
<?php
define( 'STDIN', fopen( 'php://stdin', 'r' )); 
function prompt( $msg ) 
{ 
    echo $msg . "\n"; 
    ob_flush(); 
    $in = trim( fgets( STDIN )); 
    return $in; 
} 

$key = prompt( 'Please press a key' ); 
echo "You pressed $key\n";  

?>

Does anyone have any ideas what I'm missing?

Link to comment
https://forums.phpfreaks.com/topic/300215-pause-until-keyboard-input/
Share on other sites

Um..... You must be new at this PHP stuff.

 

PHP runs on the server and doesn't have a "user interface". Consequently you cannot pause a script and await a keypress of any other user input. What you want is probably some kind of JS code that does what you want, but in that case it would be not so much of a "pause" but rather an "event handler" that gets triggered by a 'keypress' or 'onclick' event.

Um..... You must be new at this PHP stuff.

 

PHP runs on the server and doesn't have a "user interface". Consequently you cannot pause a script and await a keypress of any other user input. What you want is probably some kind of JS code that does what you want, but in that case it would be not so much of a "pause" but rather an "event handler" that gets triggered by a 'keypress' or 'onclick' event.

So you're saying it can't be done through php without using a client side scripting language. I didn't think it could but when I saw these different codes online it sure looked like people were doing it with php alone.  Can you explain how any of those codes work or are they all wrong?   

PHP does have a command-line interface, and your script works just fine when executed from the console.

 

What exactly happens on your PC? Are you saying you see all output but cannot input anything?

Thanks for the clarification,

That's exactly what I didn't understand. I didn't know that I could run php through the terminal. When i tried it through the terminal things worked fine but I was trying to run those scripts as a web page through my Apache server so there were no pauses in the execution of the script. I nested an html button to do what i needed inside the php code but I wanted to experiment to see if i could do a pause with php until the user is ready. What i'm really doing is having a script to encode a session & I wanted a pause to give the user time to write the filename down they chose before returning them to another page. 

You could simulate this 'conversation' by sending your beginning screen with a couple of buttons on it that will send the user's input back to the script. When the script is called it checks which button was clicked and decides what to do for you based upon that. Once your work is done your script can then send a new screen back to the client and await the next response.

 

(I keep forgetting about the command-line PHP. Probably because I have never used it.)

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.