0

I have been playing around with the idea of emulating a command line with javascript/PHP. The basic idea is, I want to be able to run a c++ program like I would on a command line.

I made a basic console with javascript, and a code parser for C++ to add certain things to the original source to enable my PHP program execution script to recognize when there is an input request or when the program ends. Now, I want to be able to communicate between the javascript(console web app) and PHP(program execution) in such a way that PHP can tell javascript what to display(output) and request input.

I had planned it with AJAX. Once the user would enter the program run command on the console(javascript end), it would switch to execution state and send a request to a PHP script with the program name. The script would start the process and end by echoing the result. Based on the request response, javascript would again send a request to the PHP script, which would pick off where it left and echo another result for the javascript to pick up on. And this would go on until the PHP script echoed an error string or a termination string.

After many naive attempts with AJAX, I know my idea was completely wrong. I've been wondering if there is another method to communicate between the two ends. Is there a way by which I can save my PHP script's state (resources, variables and file handles) such that whenever I send a request to it from the javascript end, I can get output continuing from where I last left off?

I haven't been able to find one yet, which leads me to believe this approach is wishful thinking.

I would really appreciate some insight.

Rikonator
  • 1,830
  • 16
  • 27
  • 3
    Have you never used [sessions](http://php.net/manual/en/features.sessions.php)? – epascarello Nov 28 '12 at 21:59
  • "Based on the request response, javascript would again send a request to the PHP script" Why is there so much automatic communication between the client side and the server side? What is this back-and-forth you are talking about? Ideally the PHP side would run until it errored or terminated. – TheZ Nov 28 '12 at 22:01
  • @TheZ I'm emulating a command line. Say I want to run a simple program which asks the user to enter a number, and then displays the number entered. So on the website, using javascript, I first want to output what the program is outputting to the script, until I know I need to send the program input, at which point I request the javascript to recieve input via the console. – Rikonator Nov 28 '12 at 22:39

3 Answers3

6

PHP Sessions

Sessions

Session support in PHP consists of a way to preserve certain data across subsequent accesses. This enables you to build more customized applications and increase the appeal of your web site. All information is in the Session reference section.

Community
  • 1
  • 1
epascarello
  • 204,599
  • 20
  • 195
  • 236
  • 1
    Thank you. I had not heard of sessions. This should, if it works, completely solve my problem. – Rikonator Nov 28 '12 at 22:04
  • Unfortunately, this did not work exactly. I cannot store resources in `$_SESSION`. Which brings me back to square one, because I can't read the program's output(with my handle to stdout) if I can't save my handle to stdout. – Rikonator Nov 28 '12 at 22:35
  • Well you need to keep a process active: http://stackoverflow.com/questions/1481247/how-to-continue-process-after-responding-to-ajax-request-in-php – epascarello Nov 28 '12 at 22:46
  • Does `ignore_user_abort` ensure that I will be able to access the same resources I created on init when I send a request to the script from javascript to get program input/output? – Rikonator Nov 29 '12 at 07:57
0

Using the PHP Sessions library (as suggested by epascarello) is probably your best bet. However, if you wanted to "roll your own", or if you just wanted to understand how libraries like PHP Sessions work, the basic gist is:

  1. If there's any data you wish to pass from your server to your client initially, write that data out to the page. For instance <script>var serverData = <?= serverDataVariable ?>; (my PHP is a bit rusty; please forgive me if that syntax is off).

  2. If there's any data you want to pass to your server at the end, put that data in an HTML form and set the action of that form to the URL of your server-side handler. This way when the user submits the form, all of the data in it will get sent to that URL (in the GET or POST parameters, depending on how you set your form).

  3. If you want to communicate any time in between 1. and 2., you need to use AJAX (well, you could use comet too, but that's advanced so don't worry about it). AJAX uses the browser's XmlHttpRequest to essentially do a form submission, only "behind the scenes" without requiring a page change. AJAX requests can provide parameters (just like form submissions), and they also return with data from the server (almost the same as if you'd written that data to the page originally).

machineghost
  • 33,529
  • 30
  • 159
  • 234
0

I would recommend websockets over AJAX, perhaps using Ratchet.

Although it's written in Node.js and not PHP, you may find some inspiration in tty.js

Good luck!

psema4
  • 3,007
  • 1
  • 17
  • 22