Implementing a CC ++ program via Web with PHP

zhaozj2021-02-17  54

Implementing a C / C program via Web with PHP

Author: Mitchell Harper

Translation: Kefeng Nie (Kefeng_nie@163.com)

Date: 2002-10-15

First, introduction

If you have aware of UNIX / Linux, you should know that they have their own compilers with C and C , named GCC and G . Unix uses these compilers in many places such as program installation and Make. With some console commands, C and PHP, I will introduce you how to generate a complete C program example, he can execute with a PHP program and obtain the corresponding output. I will be a C program code, and compile it. After talking, we will perform this program if we will use the PHP function PASSTHRU. In a sense, this article gives us a way to access general programs through a web page.

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 , UNIX console commands, of course, some PHP programming experience is also required.

Second, write a C program

For example, we can write a C simple program capable of receiving parameters through the command line, and named SampleApp. Then we can deliver three different parameters as follows:

SampleApp - Parameter 1 - Parameter 2 - Parameter 3

The function of this program is to output the number of parameters passed to him and the value of each parameter, and we can use a PHP script to perform compilation C programs.

Use the text editor you habits to create a file called SampleApp.cpp, and then 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": "" ") <<" arguement "<< (ARGC-1 == 1? "": "S") << "<< (argc-1 == 1?" IS ":" area) << ":" << Endl << endl; for (int i = 1; i < Argc; i ) cout << "[" << i << "] << argv [i] << endl; return 0;}

The entry point of the program included in this C program: main (), the main () function has brought two parameters: argc (number of command lines) and argv (a character pointer including the actual actual value of the passing parameters) Array). This two parameters can be automatically captured by the C compiler.

COUT << Endl << "You passed" << ARGC-1 << "arguement" << (argc-1 == 1? ":" s ") <<". "<< Endl ;; It means the number of parameters that are incorporated from the execution of the command line. Argv This character pointer array is retrieved from 0, which contains at least one actual value (ie, the path and name of this program), which is automatically attached by the C compiler. Conditional operator "?" Is used to determine whether the command line is incorporated in the parameter. For example, if the command line passes 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;

Next, we use the conditional operators to output another sentence. But remember, even if we don't pass any parameters from the program, the main function is also a value. Similarly, if we pass two parameters from the command line to the program, the program will output the following information:

THESE ARGUMENTS Are: for (int i = 1; i

Finally, the main function is incorporated by the output command line, which uses a simple for (;;) loop statement, which can output the parameter value according to the number of parameters. If we pass the two parameters "first" and second ", the result of the FOR cycle output is as follows:

[1] -first [2] -second

The above is a brief description of this C program, its function is very simple, is to display the parameters incorporated by the command line to the output screen.

Next, we will compile this .cpp file, if you need Telnet to the Server used under the Windows platform. Here, we use the G compiler provided on Most UNIX machines to compile this source file. However, in order to be confident that your machine is installed G , you can enter the following command: Which G . If G is already installed, UNIX shell will display the full path where G is located. If not installed, it will prompt you "Command Couldn't Be Found". You can download it here to G .

The directory input in the source file is as follows G command:

G -c Sampleapp.cpp.

With this command, we compile the .cpp file into a target file containing machine code. With the ls -a command, you can find a new file Sampleapp.o in this directory, which is the result of the .cpp source file being compiled into a machine code. But we finally want to be an executable, because we also need to enter the following G command: g Sampleapp.cpp -o sampleApp

This way we get an executable named SampleApp. However, it is noted that the executable of UNIX is different from Windows, it does not have any suffixes.

Below we can check the results of the program, if the following command:

Sampleapp one -two / three

We can see the following execution results:

You Passed 3 Arguments. Thase Arguments Are: [1] One [2] -Two [3] / three

Now, the executable C program is completed, and we will generate a PHP programs that can access this program through a web browser.

Third, generate a PHP scriptor

In order to call our C programs through the Internet, we need to generate a PHP script. This PHP script will have an Form form so that the user can enter parameters that can be transmitted to the program SAMPLEAPP. The code of the PHP script is too long, and it is not listed here. If necessary, you can download it by the following address. (PHP Code)

IF (@ $ Submit) {} else {}

First, the script checks if the variable $ Submit is worthless, this variable $ Submit's value is passed after the procedure submitted, it is default a null value. The symbol @ 的 相关 相关 错 错 错 错 错 错 错 错 错 错 错 错 错 错

Since the variable $ submit is empty, the code in the ELSE {} is initially implemented, and it is simply displayed on the browser to display an Form form. The form's action property is set to the variable $ PHP_SELF, that is, the form is submitted to this page. At the same time, the Form form contains a text input strip, which is used to allow user to enter command line parameters to pass to C programs. FORM as shown below:

Once we enter the execution command and submit a form, the variable $ Submit (ie the name of the button Go GO) gets a value so that PHP will execute the code between IF {}.

IF ($ args == "") echo "

You Didn't Enter Any Arguments. "; else {echo "

SampleApp Result "; $ comMand = "/ htdocs / Sampleapp ". Escapeeshellcmd ($ args); PASSTHRU ($ Command);

Variable $ ARGS is automatically generated, its value is the value of the text input strip in the Form form. If you do not enter any information, the program will simply tell the user that there is no value to enter any value.

If the user enters any non-empty information, the program will pass the value of the TEXT domain, that is, the variable $ args to the C program. The following section is the execution command of executing C programs:

$ comMman = "/ htdocs / sampleapp". Escapeeshellcmd ($ args);

Functions eAcapeshellcmd are used as a security check tool to filter some special characters such as ",", "/", and "//". This prevents some users from trying to enter certain characters to call the internal commands of the system.

For example, if you enter "1 -Two / Three" in the Text Domain of the Form form, the value of the variable $ command is: / htdocs / sampleapp 1 -two / three you can find that we define the full path of program SampleApp, In this example, the program file is located in the / htdocs directory. You can make a corresponding modification according to the directory where your own program is located.

PASSTHRU ($ Command);

Finally, we use the PHP's function passthru to perform the commands contained in the variable $ Command and output the original execution result to the browser. On my server, the HTML page that returns the result is as follows:

W Before this article is about to end, several problems that may come, I want to talk. First, when you perform Sampleapp.php, if you don't see any output information of the program, you may have an open security mode. If so, the system will not allow the PHP script to perform system internal programs. Regarding how to turn off safety mode, please visit http://www.php.net/manual/en/features.safe-mode.php, which has a detailed introduction. Second, in some UNIX systems, the PHP function PASSTHRU cannot pass the output of the internal program to the browse page. If this happens, you can use the System function instead of the PASSTHRU function.

Fourth, conclusion

As can be seen from this example, the UNIX operating system is very powerful, and PHP allows developers to perform system internal programs through scripts with separate threads. The example given this article is very simple, but just spend more, you can write a program that updates the Mysql database's C program, runs other system commands or the operating system file / directory structure. However, in any case, you should make sure your system security is definitely not allowing any other script to access the system internal programs.

If you want to get more information about this article, you can view some of the relevant 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.escapeeshellcmd.php

Related books:

Advanced Programming in The Unix Environment

Beginning C : The Complete Language

Linux Apache Web Server Administration

Professional apache

转载请注明原文地址:https://www.9cbs.com/read-29111.html

New Post(0)