How to use PHP to execute CC ++ programs through the Web. I. INTRODUCTION If you have some knowledge about UnixLinux, you should know that most of them come with C and C ++ compilers, GCC and G ++ respectively. Unix is used in many places such as program installation and Make. 1. Introduction
If you know something about Unix/Linux, you should know that most of them come with C and C ++ compilers, GCC and G ++ respectively. Unix uses these compilers in many places such as program installation and Make. Use some console commands, C ++ and PHP. I will introduce you to how to generate a complete C ++ program example, which can be executed in a PHP program and get corresponding output results.
I will compile the C ++ program code and 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.
To better understand this article, you should have a unix/Linux server running apache and the latest php version. At the same time, you should also master the C ++ and unix console commands. of course, some PHP programming experience is also required.
2. compile a C ++ program
For example, we can write a simple C ++ program that can receive parameters through the command line and name it Sampleapp. then we can pass three different parameters to him as follows:
Sampleapp? Parameter 1? Parameter 2? Parameter 3
The function of this program is to output the number of parameters passed to it and the value of each parameter. then we can use the PHP script program to execute the compiled C ++ program.
Create a file named Sampleapp. cpp using the text editor you are used to, and enter the following code in the 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;
For (int I = 1; I <argc; I ++)
Cout <"[" <I <"]"
<Argv [I] <endl;
Return 0;
}
The entry point of the C ++ program: main (). The main () function includes two parameters: argc (number of parameters passed in through the command line) and argv (a struct pointer array containing the actual value of the passed parameter ). These two parameters can be automatically captured by the C ++ compiler.
Cout <endl <"You passed" <argc-1
<"Arguement"
<(Argc-1 = 1? "": "S ")
<"." <Endl;
;
This statement is used to obtain the number of parameters passed in from the command line. Argv: The struct pointer array is retrieved from 0. it contains at least one actual value (the path and name of the program ), this value is automatically appended by the C ++ compiler. Conditional operator "?" Is used to determine whether there are more than one input parameter in the command line. For example, if two parameters are input through the command line, the local 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;
Next, we also use conditional operators to output another sentence. Remember that, even if we do not pass any parameters 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 are:
For (int I = 1;
I <argc; I ++)
Cout <"[" <I <"]"
<Argv [I] <endl;
Finally, the main function outputs each parameter input from the command line one by one. it uses a simple for (;) loop statement, this function outputs parameter values one by one based on the number of parameters. If we pass two parameters "first" and second "to the program, the output of the for loop 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 parameters passed in by the command line are displayed on the output screen using the cout function.
Next, we will compile this. cpp file. if you are on windows, telnet to the server you are using. Here, we use the G ++ compiler provided on most Unix machines to compile this source file. To ensure that your machine has installed G ++, run the following command: which g ++. If G ++ has been installed, Unix shell displays the full path of G ++. If it is not installed, it will prompt you to say "command couldn't be found ". You can download it to G ++. Enter the following G ++ command in the directory where the source file is located:
G ++-c sampleapp. cpp.
With this command, we compile the. cpp file into a target file containing the machine code. Through ls? A command, you can find a new file sampleapp. o appears in the local directory. this is the result of the. cpp Source file being compiled into a machine code. But what we finally want is an executable file, because we need to enter the following G ++ command:
G ++ sampleapp. cpp? O sampleapp
In this way, an executable file named sampleapp is obtained. However, the executable file in Unix is different from that in Windows, and it does not have any suffix. Next we can check the execution result of the program, if the following command:
Sampleapp one-two/three
We can see the following execution results:
You passed 3 arguments.
These arguments are:
[1] one
[2]? Two
[3]/three
Now, the executable C ++ program is generated. next we will generate a PHP Tutorial program that can access this program through a web browser.
3. generate a PHP script
To call our C ++ program over the Internet, we need to generate a PHP script program. This PHP script contains a Form that allows you to input parameters that can be passed to the Sampleapp program. The PHP script code is not listed here because it is too long. you can download it at the address below if necessary. (Php code)
If (@ $ submit)
{
}
Else
{
}
First, check whether the variable $ submit has a value. The value of this variable $ submit is passed after the Form is submitted after the program. The default value is null. Symbol @ is used to ignore related error messages when the value of variable $ submit does not exist.
Because the variable $ submit is empty by default, the code in else {} is executed at the beginning, and a Form is displayed in the browser. The action attribute of Form is set to variable $ PHP_SELF, that is, this page is returned after the Form is submitted. At the same time, the Form contains a text input entry, which is a command line parameter that allows the user to input and pass to the C ++ program. Shows the Form:
Once we enter the execution command and submit the form, the variable $ submit (that is, the name of the Go button) will get a value, so that the PHP Tutorial will execute 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. its value is the value passed by text entries in the Form. If no information is entered, the program will simply tell the user that no value is entered.
If you enter any non-empty information, the program will pass the value of the text field, namely the variable $ args, to the C ++ program. The following code is the execution command of the C ++ program:
$ Command = "/htdocs/sampleapp"
. Escapeshellcmd ($ args );
The eacapeshellcmd function is used as a security check tool to filter special characters such as ",", "", and. This prevents some users from attempting to enter certain characters to call internal system commands.
For example, if you enter "1? Two/three ", then the value of the variable $ command is:
/Htdocs/sampleapp 1? Two/three
You can find that we have defined the full path of the sampleapp program. In this example, the program file is located in the/htdocs directory. You can modify the directory where your program is located.
Passthru ($ command );
Finally, we use the PHP function passthru to execute the command contained in the variable $ command and output the original execution result to the browser.
W. before the end of this article, I would like to discuss several possible problems. First, when you execute sampleapp. php to teach the program, if you do not see any output information of the program, you may have enabled the security mode. In this case, the system will not allow PHP scripts to execute internal system programs.
For details about how to disable the security mode, visit http://www.php.net/manual/en/features.safe-mode.php. Secondly, on some Unix systems, the PHP function passthru cannot pass the internal program output to the browser page. in this case, the system function can be used to replace the passthru function.
IV. Conclusion
From this example, we can see that the Unix operating system is very powerful, and PHP allows developers to execute internal programs in the system using scripts in separate threads. The example given in this article is very simple, but as long as you spend more time, you can write a c ++ program that can update the Mysql database, programs that run other system commands or operating system files/directory structures. However, in any case, you should ensure the security of your system and never allow any other script programs to access internal programs.
If you know something about Unix/Linux, you should know that most of them come with C and C ++ compilers, GCC and G ++ respectively. Unix is used in many places such as program installation and Make...