Skip to content
Advertisement

How to interact between webpage and an actual program

What I have:

  • A program (c++) on a server (linux)
  • The program can be executed with command line parameters
  • The program finishes after a while and produces continously output (“log.txt”)

What I want:

My idea is to have some kind of user interaction with that program:

  1. The user opens a website which shows some inputs (checkboxes, etc.)
  2. After submitting (POST) to the server the program should be started with chosen settings
  3. While running log.txt changes made by the program should be displayed to the user
  4. When finished a message should occur (e.g. “Done”)

What would be the best approach? My current idea is to start a background process (php execute a background process ) and monitor the process id. However, I do not know how to dynamically monitor the process with php, how to show the user the log.txt output or how to tell the user that the program has finished, because these things are not static but dynamic.

I am quite fine with C++, but my html and php skills are basic. Maybe I need a further technology for this?

Advertisement

Answer

<?php 

ob_implicit_flush(true);
ob_end_flush();

$cmd = "your_program -{$_POST[option1]} -{$_POST[option2]}";

$descriptorspec = array(
   0 => array("pipe", "r"),
   1 => array("pipe", "w"),
   2 => array("pipe", "w")
);
flush();
$process = proc_open($cmd, $descriptorspec, $pipes, realpath('./'), array());
echo "<pre>";
if (is_resource($process)) {
    while ($s = fgets($pipes[1])) {
        print $s;
        flush();
    }
}
echo "</pre>";

proc_close();

echo 'Proc finished!';

as answered here, but with a few modifications.

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement