Execute C/C ++ applications on the Web in PHP

Source: Internet
Author: User

Introduction: This is a PHP application that executes 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 = 324600 'rolling = 'no'>

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, which can be executed using the PHP program, and get the corresponding output results. I will convert Mr into a C ++ Program Code And compile it. After talking about it, we will execute this program by using the PHP function Passthru. In a sense, here Article It 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 <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 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 ++ here.

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 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 "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 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. 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.

More articles on "executing C/C ++ applications through web in PHP"

Love J2EE follow Java Michael Jackson video station JSON online tools

Http://biancheng.dnbcw.info/php/324600.html pageno: 14

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

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.