Apache
  Home arrow Apache arrow Page 3 - Executing A C++ Application Over The Inter...
Dev Articles Forums 
ADO.NET  
Apache  
ASP  
ASP.NET  
C#  
C++  
ColdFusion  
COM/COM+  
Delphi-Kylix  
Design Usability  
Development Cycles  
DHTML  
Embedded Tools  
Flash  
Graphic Design  
HTML  
IIS  
Interviews  
Java  
JavaScript  
MySQL  
Oracle  
Photoshop  
PHP  
Reviews  
Ruby-on-Rails  
SQL  
SQL Server  
Style Sheets  
VB.Net  
Visual Basic  
Web Authoring  
Web Services  
Web Standards  
XML  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
APACHE

Executing A C++ Application Over The Internet With PHP
By: Mitchell Harper
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 21
    2001-12-27

    Table of Contents:
  • Executing A C++ Application Over The Internet With PHP
  • Creating the C application
  • Creating the PHP script
  • Conclusion

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    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.

    More Apache Articles
    More By Mitchell Harper


       · Thank you very much for writing this article. I was trying to learn how to do just...
     

    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







    © 2003-2010 by Developer Shed. All rights reserved. DS Cluster 4 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek