Home arrow Apache arrow Page 3 - Executing A C++ Application Over The Internet With PHP
APACHE

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.

Author Info:
By: Mitchell Harper
Rating: 4 stars4 stars4 stars4 stars4 stars / 29
December 27, 2001
TABLE OF CONTENTS:
  1. · Executing A C++ Application Over The Internet With PHP
  2. · Creating the C application
  3. · Creating the PHP script
  4. · Conclusion

print this article
SEARCH DEVARTICLES

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.

if(@$submit)

{



}

else

{



}


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:

The HTML form that accepts the command-line arguments

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:

if($args == "")

echo "<h1>You didn't enter any arguments.</h1>";

else

{

echo "<h1>SampleApp Result</h1>";

$command = "/htdocs/sampleapp " . escapeshellcmd($args);

passthru($command);

}


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:

The output of our script when the text field is blank

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):

$command = "/htdocs/sampleapp " . escapeshellcmd($args);

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.

passthru($command);

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:

Output from our sampleapp program

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.
blog comments powered by Disqus
APACHE ARTICLES

- Programmatically Manipulating Microsoft Exce...
- Installing PHP under Windows
- Compressing Web Content with mod_gzip and mo...
- Compressing Web Output Using mod_deflate and...
- Setting Up Apache 2.0.45 to Parse PHP Pages
- Custom Error 404 Documents with PHP
- Using Apache and PHP on Mac OS X
- ASP: Active Sessions, Active Logins and Tota...
- Working With Oracle on Windows: Part 1
- The Quick-n-Dirty Guide to Setting Up Apache...
- Installing Apache With SSL: The Complete Gui...
- 7 Powerful .htaccess Customization Tips
- Trap And Get Notified: A Practical Solution ...
- One Way To Use Server Side Includes
- Using ForceType For Nicer Page URLs

Watch our Tech Videos 
Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 
Support 

Developer Shed Affiliates

 




© 2003-2017 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials