Frage

I'm trying to figure out if it is possible to do the following using php:

I have a website that uses php to display database information. Now, I'd like to be able to implement another capability and that is I'd like to write a new php web page and launch a thread which executes a binary and have the output be displayed on the web page live (if possible).

I think I need to use ajax to be able to refresh the page in order to show "live output" in the page.

The problem I'm having is figuring out if I can launch a thread that executes a binary and display the output on the webpage. Is that possible? If so, how?

War es hilfreich?

Lösung

You can do like this, use jquery plugin, to do ajax and load result of script.php to any section of your html page.

script.php

<?php
     echo exec('date'); //type command to be executed here.
?>

page.html

<html>
    <head>
    <script type="text/javascript" src="http://code.jquery.com/jquery-1.11.0.min.js" >
    </script>
    <script type="text/javascript">
    $(document).ready(function(){
        setInterval(executeScript,1000);
    });

    function executeScript(){
        $("#result").load("script.php");
    }
    </script>

</head>
<body>
    <div id="result"> command result will come here.. </div>
</body>
</html> 

It will call script.php at every second and its result will be shown in <div id="result"> </div>

You can also use jQuery ajax .post(), .get() or .ajax() to pass some argument to script and get response accordingly.

Andere Tipps

The way to do this kind of thing is usually to execute the binary and disown it but have its output written to a file as it runs.

Then your ajax system literally downloads that text file for display.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top