Introduction: PHP is used to execute C/C through the Web.ProgramThe details page of PHP, related knowledge, skills, experience, and some PHP source code.
Class = 'pingjiaf' frameborder = '0' src = 'HTTP: // biancheng.dnbc?info/pingjia.php? Id = 323728 'rolling = 'no'>
Author: Mitchell Harper
Kefeng nie, kefeng_nie@163.com)
Date: 2002-10-15
I. 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. Using some Console Commands, C and PHP, I will introduce you to how to generate a complete C program example. He can execute it using the PHP program and obtain the corresponding output results. I will convert Mr into C programCodeAnd compile it. After talking about it, we will execute this program by using the PHP function Passthru. In a sense, hereArticleIt 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 necessary.
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 it in the following way:
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 <iostream. h>
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 program contained in this 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), which 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] seconsecond
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 the 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 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 entry, which is used to allow the user to input the command line parameters to be passed 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 "Else
{
Echo "$ 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 out 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" in the text field of the Form, the value of $ 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. On my server, the returned HTML page is as follows:
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.
For more information, see the links and books listed below.
Related links:
Http://galton.uchicago.edu /~ Gosset/compdocs/gcc.html
Http://www.andrews.edu /~ Maier/tutor.html
Http://www.php.net/manual/en/function.escapeshellcmd.php
Related books:
Advanced Programming in the Unix environment
Beginning C: The complete language
Linux Apache Web server administration
Professional Apache
More articles on "using PHP to execute C/C Programs on the Web"
Love J2EE follow Java Michael Jackson video station JSON online tools
Http://biancheng.dnbcw.info/php/323728.html pageno: 16.