Executing A C++ Application Over The Internet With PHP
Unix is a powerful operating system that uses C/C++ extensively. In this article, Mitchell will use a couple of console commands, some C++ and some PHP, to show you how to create a totally integrated C++ application, which can be executed (and have its output captured) from PHP.
Executing A C++ Application Over The Internet With PHP - Creating the PHP script (Page 3 of 4 )
To call our compiled C++ application over the Internet, we will create a PHP script. This PHP script will display a HTML form into which we can enter arguments to pass to the “sampleapp” executable. The entire code for the PHP script is a bit too long to list, so you can download it as part of the support material for this article, from the last page.
Firstly, our script checks to see whether the $submit variable contains a value. The $submit variable is passed from the form submitted at the bottom of the script, and by default, will be empty. The @ symbol stops PHP from spitting an error if the $submit variable doesn’t exist.
Because the $submit variable is empty by default, the code between the “else” braces is executed. This simply outputs a HTML form to the web browser. The “action” attribute of the form is set to the $PHP_SELF variable, which will submit the form back to itself. The form contains one text field, into which we will enter the command-line arguments to pass to our C++ application. The form looks like this:
Once we enter some arguments and submit the form, the $submit variable (which is the name of the “Go” button) will contain a value. This will cause our PHP script to execute the code between the “if” braces:
The $args variable is automatically created and given the value of the text field in our HTML form. If no arguments were entered, we simply tell the user that they didn’t enter any:
On the other hand, if they entered at least one character into the text field, then we pass that text fields value, $args, to our C++ application. Because we will be executing our C++ application, we create the command that we will actually execute (more on this in a minute):
The escapeshellcmd function is used as a safety measure, and escapes characters such as ‘, “ and \ with \’, \” and \\. This prevents users from entering any arguments that could trigger internal Unix commands.
If we entered “1 –two /three” into the text field of our HTML form, for example, then our $command variable would contain:
/htdocs/sampleapp 1 –two /three
You can see that we are specifying the full path to our sampleapp file. In this example, mine is located in the “/htdocs” directory. You should change this to match the full path to the sampleapp file on your server.
Lastly, we use the passthru PHP function, which will execute our $command variable as an external Unix program and pass the raw output back to our web browser. On my server, the resultant HTML page looks like this:
I just want to mention a couple of things before I wrap this article up. Firstly, if you don’t receive any output from your program when you run the sampleapp.php script on your web server, safe mode may be on. This will stop execution of external programs from within a PHP script. Visit http://www.php.net/manual/en/features.safe-mode.php for details on turning safe mode off.
Also, on some Unix systems, the PHP function passthru doesn’t send the output from the external program back to the web browser. If this is the case for you, try using the system function instead.