Using PHP to implement a C + + program through the Web

Source: Internet
Author: User
Tags array execution html page php code php programming variable access mysql database
C++|web| Program | Executive One, INTRODUCTION

If you have any knowledge of unix/linux, you should know that most of them bring in C and C + + compilers, gcc and g++ respectively. 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 C + + program, which he can execute with PHP programs and get the corresponding output. I will be Mr. C + + program code and compile it, talk after discussion we will execute this program by using PHP's function PassThru. In a sense, this article gives us a way to access general programs through Web pages.

To get a better understanding of this article, you should have a Unix/linux server running Apache and the latest version of PHP. Also should master C + +, UNIX console commands, of course, some PHP programming experience is also necessary.

Second, write a C + + program

For example, we can write a C + + simple program that can also receive parameters through the command line, named SampleApp. Then we can pass three different parameters to him in the following way:

SampleApp? parameter one 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 then enter the following code in this 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 << Endl;

for (int i = 1; i < argc; i++)
cout << "[<< I <<"] "<< argv[i] << Endl;

return 0;

The entry point of the program that this C + + program contains: Main (), and the main () function takes two parameters: ARGC (the number of incoming arguments in the command line) and argv (a character pointer array that contains the actual value of the passed parameter). 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 array of character pointers is retrieved from 0 and contains at least one actual value (that is, the path and name of this program), which is automatically appended by the C + + compiler. Conditional operator "?" is used to determine whether the command line passes in more than one parameter. 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;

Next, we also use the conditional operator to output a different sentence. Remember, however, that even if we don't pass in any arguments from the program execution command line, the main function argv[] parameter also contains a value. Similarly, if we pass two arguments 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 of the arguments passed by the command line, and it uses a simple for (;;) Loop statement, which can output a parameter by one value based on the number of parameters. If we pass the program two parameters "first" and "second", the result of the For loop output is as follows:

[2]? second

The above is a simple description of the C + + program, its function is very simple, that is, the command line passed the parameters of the cout function displayed on the output screen.

Next, we will compile this. cpp file, and if you are on the Windows platform, you need to telnet to the server you are using. Here we compile the source file using the g++ compiler provided on most UNIX machines. However, to make sure that your machine is g++, you can enter the following command: which g++. If the g++ is already installed, the Unix shell will display the full path of the g++. If it is not installed, it prompts 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 file resides:

g++-C sampleapp.cpp.

With this command, we compile the. cpp file into the target file containing the machine code. With the 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 ultimately want is an executable file, because we also need to enter the following g++ command:

g++ sampleapp.cpp o SampleApp

This allows us to obtain an executable file called SampleApp. Note, however, that the executable file under UNIX is not the same as windows, and it does not have any suffixes.

Here we can check 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 are:

[1] One
[2]? two

Now that the executable C + + program Genetic finished, we will build a PHP textbooks program that can access the program through a Web browser.

Third, generate PHP script program

In order to be able to invoke our C + + program over the Internet, we need to generate a PHP script program. The PHP script will have a form that allows users to enter parameters that can be passed to the program SampleApp. The code for the PHP script is too long to be listed here and can be downloaded from the address below. (PHP code)

if (@ $submit)


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 is submitted after the program, which defaults to a null value. The action of the symbol @ is to ignore the associated error message when the value of the variable $submit does not exist.

Because the variable $submit defaults to NULL, the code in the else{} is executed at the beginning, and it simply displays the form form on the browser. The form's Action property 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 allow the user to enter command-line arguments to be passed to the C + + program. The form is shown in the following illustration:

Once we enter the execution command and submit the form, the variable $submit (that is, the name of the button go) Gets a value so that the PHP textbooks will execute the code between if{}.

if ($args = = "")
echo "Else
echo "$command = "/htdocs/sampleapp". Escapeshellcmd ($args);

PassThru ($command);

The variable $args is generated automatically, and its value is the value of the text input bar in form forms. If no information is entered, the program simply tells the user that no value has been entered.

If the user enters any non-empty information, the program passes the value of the text field, which 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);

Function Eacapeshellcmd is used as a security check tool to filter 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, the value of the variable $command is:/htdocs/sampleapp 1? two/three

You can see that we have defined 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 of your own program.

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 issues that may be encountered. First of all, when you execute sampleapp.php textbooks program, if you do not see any output information of the program, perhaps the security mode is open. 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 page Second, on some UNIX systems, PHP functions PassThru cannot pass the internal program output to the browsing page, and if this happens, you can replace the PassThru function with the system function.

Four, conclusion

As you can see from this example, the UNIX operating system is very powerful, and PHP allows developers to execute the system's internal programs through scripts with separate threads. The examples given in this article are very simple, but with a little more effort, you can write a C + + program that updates the MySQL database, a program that runs other system commands, or an operating system file/directory structure. However, you should make sure that your system is secure and that no other scripting program can access the system's internal programs at random.

If you want to get more information about this article, check out some of the relevant links and books listed below.

Related Article

Alibaba Cloud 10 Year Anniversary

With You, We are Shaping a Digital World, 2009-2019

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

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: and provide relevant evidence. A staff member will contact you within 5 working days.