Home >Backend Development >PHP Tutorial >Execute C/C++ applications through the Web in PHP_PHP Tutorial
1. Introduction
If you know something about Unix/Linux, you should know that most of them come with C and C++ compilers, which are GCC and G++ respectively. Unix uses these compilers in many places such as program installation and Make. Using some console commands, C++ and PHP, I will show you how to generate a complete C++ program example that can be executed in a PHP program and get the corresponding output results. I will first generate the C++ program code, compile it, and then discuss how we will execute this program by using the PHP function passthru. In a sense, this article provides us with a way to access general programs through Web pages.
In order to better understand this article, you should have a unix/Linux server running apache and the latest version of php. At the same time, you should also master C++ and unix console commands. Of course, some PHP programming experience is also required.
2. Write a C++ program
For example, we can write a simple C++ program that can also receive parameters through the command line, and name it Sampleapp. Then we can Pass him three different parameters in the following way:
Sampleapp?Parameter one?Parameter two?Parameter three
The function of this program is to output the parameters passed to him The number and the value of each parameter, then we can use the PHP script program to execute the compiled C++ program.
Use your favorite text editor to create a new file named Sampleapp.cpp, and enter the following code into this file: #include <iostream.h> <code> #include <iostream.h><br> <br> int main(int argc, char* argv[])<br> {<br> cout << endl << "You passed " << argc-1 << " arguement"<BR> << (argc-1 == 1 ? "" : "s") << "." << endl;<BR> <BR> cout << (argc-1 == 1 ? "This" : "These")<BR> << " arguement" << (argc-1 == 1 ? "" : "s") << " "<BR> << (argc-1 == 1 ? "is" : "are") << ": " << endl << endl;<BR> <BR> for(int i = 1; i < argc; i++)<BR> cout << "[" << i << "] " << argv[i] << endl;<BR> <BR> return 0;<BR> }
int main(int argc, char* argv[])
{
cout << endl << "You passed " << argc-1 << " argument" cout << endl << "You passed " << argc-1 << " arguement"<BR> << (argc-1 == 1 ? "" : "s") << "." << endl;;
<< (argc-1 == 1 ? "" : "s") << "." << endl;
cout << ( argc-1 == 1 ? "This" : "These")
<< " argumentment" << (argc-1 == 1 ? "" : "s") << " "
<< (argc-1 == 1 ? "is" : "are") << ": " << endl << endl;
cout << (argc-1 == 1 ? "This" : "These")<BR> << " arguement" << (argc-1 == 1 ? "" : "s") << " "<BR> << (argc-1 == 1 ? "is" : "are") << ": " << endl << endl;
for(int i = 1; i < argc; i++)
cout << "[" << i << "] " << argv[i] << endl;
cout << endl << "You passed " << argc-1 << " argumentment"</font> << (argc-1 == 1 ? "" : "s") << "." << endl;;
cout << (argc-1 = = 1 ? "This" : "These")<🎜> << " argumentation" << (argc-1 == 1 ? "" : "s") << " "<🎜> < ;< (argc-1 == 1 ? "is" : "are") << ": " << endl << endl;
<🎜><🎜>This news has a total of <🎜>3<🎜> pages, currently on page <🎜>1<🎜> <🎜>1<🎜> 2 3 <🎜>Next, we also use conditional operators to output another sentence. But remember, even if we don't pass in any parameters from the program execution command line, the argv[] parameter of the main function contains a value. Similarly, if we pass two parameters to the program from the command line, the program will output the following information:
for(int i = 1; i < argc; i++)<CODE> for(int i = 1; i < argc; i++)<BR> cout << "[" << i << "] " << argv[i] << endl;
cout << "[" << i << "] " << argv[i] << endl; g++ -c sampleapp.cpp.
g++ sampleapp.cpp ?o sampleapp
Next we can check the results of program execution, if the following command: sampleapp one -two /three
You passed 3 arguments. These arguments are:
[1] one
[2] ? two
[3] /three
Now that the executable C++ program has been generated, we will generate a PHP tutorial program that can access this program through a web browser.
This news has a total of
3
2
3. Generate PHP script program
In order to call our C++ program through the Internet, we need to generate A PHP script program. This PHP script will have a Form so that the user can enter parameters that can be passed to the Sampleapp program. The code of the PHP script is too long so I won’t list it all here. If necessary, you can download it from the address below. (Php code)
if(@$submit)<CODE> if(@$submit)<BR> {<BR> <BR> }<BR> else<BR> {<BR> }
{ if($args == "")<BR> echo "<h1>You didnt enter any arguments.</h1>";<br> else<br> {<br> echo "<h1>SampleApp Result</h1>";<br> $command = "/htdocs/sampleapp " . escapeshellcmd($args);<br> <br> passthru($command);<br> }
Since the variable $submit is empty by default, the code in else{} is initially executed, which simply displays a Form on the browser. The action attribute of the Form is set to the variable $PHP_SELF, that is, this page is returned after the form is submitted. At the same time, the Form form contains a text input bar, which is used to allow users to enter command line parameters to be passed to the C++ program. Form is as shown below: if($args == "")<br> echo "<h1>You didn't enter any arguments.</h1>";<code> $command = "/htdocs/sampleapp " . escapeshellcmd($args);
elseThe function eacapeshellcmd is used as a security check tool to filter calls such as ",", "" and "", etc. special characters. This prevents some users from trying to enter certain characters to invoke internal system commands.
http://www.bkjia.com/PHPjc/532648.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/532648.htmlTechArticle1. Introduction If you know something about Unix/Linux, you should know that most of them come with C and C++ compilers are GCC and G++ respectively. Unix is used in many places such as program installation and Make...