_php tutorial for executing A/C + + application through the Web in PHP

Source: Internet
Author: User
First, Introduction
  
If you know anything about unix/linux, you should be aware that most of them have their own C and C + + compilers, namely GCC and g++. UNIX uses these compilers in many places, such as program installation and make. With some console commands, C + + and PHP, I'll show you how to generate a complete example of a C + + program that can be executed with a PHP program and get the corresponding output. I'll make a C + + program code and compile it and talk about what we'll do if we use PHP's function PassThru to execute this program. In a sense, this article provides us with a way to access a general program through a Web page.
  
In order to get a better understanding of this article, you should have a Unix/linux server running Apache and the latest version of PHP. You should also be familiar with C + +, UNIX console commands, and of course some PHP programming experience is also required.
  
   Second, write a C + + program
  
For example, we can write a C + + simple program that can receive parameters via the command line and name SampleApp. Then we can pass three different parameters to him in the following way:
  
SampleApp? parameter two? parameter three
  
The function of this program is to output the number of parameters passed to him and the value of each parameter, then we can use the PHP script program to execute the compiled C + + program.
  
Using your custom text editor, create a new file named Sampleapp.cpp, and enter the following code in this file:
  
  #include
  
   int main(int argc, char* argv[])
   {
   cout << endl << "You passed " << argc-1 << " arguement"
  << (argc-1 == 1 ? "" : "s") << "." << endl;
  
   cout << (argc-1 == 1 ? "This" : "These")
  << " arguement" << (argc-1 == 1 ? "" : "s") << " "
  << (argc-1 == 1 ? "is" : "are") << ": " << endl << endl;
  
   for(int i = 1; i < argc; i++)
   cout << "[" << i << "] " << argv[i] << endl;
  
   return 0;
   }

This C + + program contains the entry point of the program: Main (), the main () function with two parameters: ARGC (the number of command-line parameters passed in) and argv (a character-type pointer array containing the actual values of the arguments passed). This two parameter can be automatically captured by the C + + compiler.
  
  cout << endl << "You passed " << argc-1 << " arguement"
  << (argc-1 == 1 ? "" : "s") << "." << endl;;
  
This means getting the number of arguments passed in from the execution command line. argv this character-type pointer array is retrieved from 0, which contains at least one actual value (the path and name of the program), which is automatically appended by the C + + compiler. Conditional operator "?" is used to determine if there are more than one parameter in the command line. For example, if the command line passes through two parameters, our program will output the following information:
  
You passed 2 arguments.
  
  cout << (argc-1 == 1 ? "This" : "These")
  << " arguement" << (argc-1 == 1 ? "" : "s") << " "
  << (argc-1 == 1 ? "is" : "are") << ": " << endl << endl;

This news a total of 3 pages, currently on the 1th page 1 2 3


Next, we also use the conditional operator to output another sentence. Keep in mind, though, that even if we don't pass any arguments from the program execution command line, the main function argv[] parameter also contains a value. Similarly, if we pass two parameters to the program from the command line, the program will output the following information:
  
These arguments is:
  
  for(int i = 1; i < argc; i++)
   cout << "[" << i << "] " << argv[i] << endl;
  
Finally, the main function outputs each of the arguments passed in the command line, which uses a simple for (;;) The loop statement, the function can be the number of parameters to the parameter value of one output. If we pass the program two parameters "first" and "second", the result of the For loop output is as follows:
  
[1] First
[2]? second
  
The above is a simple description of the C + + program, its function is very simple, that is, the command line passed in parameters with the cout function displayed on the output screen.
  
Next, we will compile this. cpp file, and if you are under Windows platform, you need to telnet to the server you are using. Here, we compile this source file using the g++ compiler that is available on most Unix machines. However, in order to be sure that your machine has g++ installed, you can enter the following command: which g++. If g++ is already installed, the Unix shell will show the full path where g++ is located. If it is not installed, it will prompt you to say "command couldn ' t be found". You can download it here to g++.
  
Enter the following g++ command in the directory where the source files are located:
  
  g++ -c sampleapp.cpp.
With this command, we compile the. cpp file into a target file that contains machine code. With LS-a command, you can see that a new file SAMPLEAPP.O appears in this directory, which is the result of the. cpp source file being compiled into machine code. But what we end up wanting is an executable file, because we're going to enter the following g++ command:
  
  g++ sampleapp.cpp ?o sampleapp   
This allows us to obtain an executable file named SampleApp. Note, however, that the executable file under UNIX is not the same as windows, and it does not have any suffixes.
  
Below we can examine the results of the program execution, if the following command:
  
  sampleapp one -two /three


We can see the following execution results:
  
You passed 3 arguments.
These arguments is:
  
[1] One
[2]?
[3]/three
  
Now that the executable C + + program Genetic is complete, we will generate a PHP textbook program that can access the program through a Web browser.

This news a total of 3 pages, currently on the 2nd Page 1 2 3


  Third, generate PHP script program
  
In order to be able to invoke our C + + program over the Internet, we need to generate a PHP scripting program. This PHP script will have a form form so that the user can enter parameters that can be passed to the program SampleApp. The code for the PHP script is too long to be listed here, so you can download it from the address below if you need it. (PHP code)
  

  if(@$submit)
   {
  
   }
   else
   {
   }
 
First, the script checks to see if the variable $submit has a value, and the value of the variable $submit is passed after the form form after the program is submitted, and it defaults to a null value. The function of the @ symbol is to ignore the associated error message when the value of the variable $submit does not exist.
  
Since the variable $submit default is empty, the code in else{} is initially executed, which simply displays a form form on the browser. The Action property of the form is set to the variable $php_self, which is returned to this page after the form is submitted. The form form also contains a text entry bar, which is used to let the user enter command-line arguments to be passed to a C + + program. The form looks like this:
  
Once we enter the Execute command and submit the form, the variable $submit (the name of the button go) Gets a value so that PHP textbook executes the code between if{}.
  
  if($args == "")
   echo "

You didnt enter any arguments.

";
   else
   {
   echo "

SampleApp Result

";
   $command = "/htdocs/sampleapp " . escapeshellcmd($args);
  
   passthru($command);
   }
  
The variable $args is automatically generated, and its value is the value passed from the text input bar of the form form. If no information is entered, the program simply tells the user not to enter any values.
  
If the user enters any non-empty information, the program passes the value of the text field, that is, the variable $args to the C + + program. The following code is the execution command for a program that executes C + +:
  
  $command = "/htdocs/sampleapp " . escapeshellcmd($args);
The function eacapeshellcmd is used as a security check tool to filter out some special characters such as ",", "" "and" ". This prevents some users from attempting to enter certain characters to invoke system internal commands.
  
For example, if you enter "1? Two/three" in the text field of the form form, then the value of the variable $command is:/htdocs/sampleapp 1? two/three
  
You can see that we define the full path of the program SampleApp, in this case the program file is located in the/htdocs directory. You can make the corresponding changes according to the directory in which your program is located.
  
PassThru ($command);
  
Finally, we use PHP's function PassThru to execute the commands contained in the variable $command and output the original execution results to the browser. On my server, the HTML page that returns the results is as follows:
  
W Before the end of this article, I would like to say a few questions that may be encountered. First of all, when you execute the sampleapp.php textbook program, if you do not see any output information of the program, it may be in safe mode. If so, the system will not allow PHP scripts to execute system internal programs. For more information on how to turn off Safe mode, please visit the Web http://www.php.net/manual/en/features.safe-mode.php

http://www.bkjia.com/PHPjc/532648.html www.bkjia.com true http://www.bkjia.com/PHPjc/532648.html techarticle I. Introduction if you know something about unix/linux, you should know that most of them have their own C and C + + compilers, gcc and g++, respectively. UNIX is used in many places such as program installation and make ...

  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.