Reputation: 1860
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.
Upvotes: 0
Views: 538
Reputation: 3047
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!
Upvotes: 0
Reputation: 35790
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:
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).
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).
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).
Upvotes: 0
Reputation: 207511
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.
Upvotes: 6