通过Web执行C/C++应用程序
一、简介
如果你对Unix/Linux有所了解的话,你应该知道他们大都自带了C和C++的编译器,分别是GCC和G++。Unix在程序安装及Make等许多地方使用到了这些编译器。利用一些控制台命令,C++和PHP, 我将向你介绍怎样生成一个完整的C++程序例子,他可以在用PHP程序来执行,并能获得相应的输出结果。我将先生成C++程序代码,并编译它,谈后讨论我们将如果通过使用PHP的函数passthru来执行这个程序。从某种意义上来说,这边文章给我们提供一种通过Web页面来访问一般程序的方法。
为了能更好的理解这篇文章,你应该有一台运行着apache和最新版本php的unix/Linux服务器。同时也应该掌握C++, unix控制台命令,当然一些PHP的编程经验也是必需的。
二、编写一个C++程序
例如,我们可以写一个能够通过命令行还接收参数的C++ 简单程序,并命名为Sampleapp.然后我们能够按照下面的方式给他传递三个不同的参数 :
Sampleapp ?参数一 ?参数二 ?参数三
这个程序的功能是能输出传递给他的参数的个数和每个参数的值,然后我们可以用PHP脚本程序来执行编译好的C++程序。
利用你习惯的文本编辑器,新建一个名为Sampleapp.cpp 的文件,再此文件中输入如下的代码:
以下为引用的内容: #include int main(int argc, char* argv[]){ cout
cout
for(int i = 1; i cout return 0; } |
这个C++程序包含的程序的入口点:main(),main()函数带了两个参数:argc(命令行传入参数的个数)和argv(一个包含了所传参数实际值的字符型指针数组)。这个两个参数能被C++编译器自动捕获。
以下为引用的内容: cout |
这句话的意思是获得从执行命令行传入的参数的个数。Argv这个字符型指针数组是从0开始检索的,它至少包含一个实际的值(即本程序的路径和名称),这个值由C++编译器自动地附加上去。条件操作符”?”是用来判断命令行传入地参数是否多于一个。例如,如果命令行过传入两个参数,我们地程序将输出如下信息:
以下为引用的内容: You passed 2 arguments. cout
|
接下来,我们同样用条件操作符来输出另一句话。不过要记住,即使我们不从程序执行命令行传入任何参数,main函数地argv[]参数也包含一个值。同样地,如果我们从命令行传入两个参数给程序,程序将输出如下地信息:
以下为引用的内容: These arguments are: for(int i = 1; i cout |
最后,main函数逐一的输出命令行传入的每个参数,它用到了一个简单的for(;;)循环语句,这个函数能根据参数的个数将参数值一个一个的输出。假如我们传给程序两个参数”first”和second”, for循环输出的结果如下:
[1] ?first
[2] ?second
以上是关于这个C++程序的简单说明,它的功能十分简单,就是将命令行传入的参数用cout函数显示在输出屏幕上。
接下来,我们将编译这个.cpp文件,如果你在windows平台下,需要telnet到所使用的server上。在这里,我们使用大多Unix机器上都提供的G++编译器来编译这个源文件。不过为了确信你的机器安装了G++,你可以输入如下命令:which g++。如果G++已经安装了,Unix shell将显示出G++所在的全路径。如果没有安装,它将提示你说”command couldn’t be found”. 你可以在这里下载到G++.
在源文件所在的目录输入如下G++命令:
g++ -c sampleapp.cpp.
通过这个命令,我们就将.cpp文件编译成了包含机器代码的目标文件。通过 ls ?a命令,你可以发现在本目录下出现了一个新文件sampleapp.o,这就是.cpp源文件被编译成机器码的结果。不过我们最终想要的是一个可执行文件,因为我们还要输入如下的G++命令:
g++ sampleapp.cpp ?o sampleapp
这样我们就获得了一个名为sampleapp的可执行文件。不过注意的是,Unix下的可执行文件跟Windows不一样,它没有任何后缀。
下面我们可以来检验一下程序执行的结果,如果如下命令:
sampleapp one -two /three
我们可以看到如下的执行结果:
以下为引用的内容: You passed 3 arguments. These arguments are: [1] one [2] ?two [3] /three |
现在,可执行的C++程序成生完毕,下面我们将生成一个能够通过 web浏览器来访问这个程序的PHP教本程序。
三、生成PHP脚本程序
为了能通过Internet来调用我们的C++程序,我们需要生成一个PHP脚本程序。这个PHP脚本程序将有一个Form表单,以便用户能输入可以传给程序Sampleapp的参数。PHP脚本的代码太长就不在这里全部列出了,需要的话可以通过下面的地址来下载它。(Php code)
以下为引用的内容: if(@$submit){ } else{ } |
首先,脚本程序检查看变量$submit是否有值,这个变量$submit的值是程序后面的Form表单提交后传递过来的,它缺省为空值。符号@的作用是当变量$submit的值不存在的时忽略相关的错误信息。
由于变量$submit缺省为空,所以一开始执行else{}中的代码,它在浏览器上简单的显示一个Form表单。Form的action属性设为变量$PHP_SELF,即表单提交后返回本页。同时Form表单包含了一个文本输入条,这是用来让用户输入要传递给C++程序的命令行参数。Form如下图所示:
一旦我们输入执行命令并提交表单,变量$submit(即按钮Go的名字)就获得一个值,这样PHP教本将执行if{}之间的代码。
以下为引用的内容: if($args == "") echo " You didn't enter any arguments. "; else{ echo " SampleApp Result "; $command = "/htdocs/sampleapp " . escapeshellcmd($args); passthru($command); } |
变量$args是自动产生的,它的值是Form表单中文本输入条传过来的值。如果没有输入任何信息,程序将简单的告诉用户没有输入任何值。
如果用户输入任何非空的信息,程序将把text域的值,即变量$args传给C++程序。下面这段代码就是执行C++的程序的执行命令:
$command = "/htdocs/sampleapp " . escapeshellcmd($args);
函数eacapeshellcmd是用来当做安全检查工具,以过滤调一些如”,”,””和”\”等的特殊字符。这可以防止一些用户企图输入某些字符来调用系统内部命令。
例如,如果你在Form表单的text域中输入”1 ?two /three”,那么变量$command的值就为: /htdocs/sampleapp 1 ?two /three
你能发现我们定义了程序sampleapp的全路径,在这个例子中,程序文件位于/htdocs目录下。你可以根据的自己程序所在的目录做相应的修改。
passthru($command);
最后,我们使用PHP的函数passthru来执行变量$command所包含的命令并且将原始的执行结果输出到浏览器上。在我的服务器上,返回结果的HTML页面如下:
w在本文即将结束之前,几个可能碰到的问题我想说一下。首先,当你执行sampleapp.php教本程序的时候,如果你没有看到程序的任何输出信息,或许是开了安全模式。如果这样,系统将不会允许PHP脚本来执行系统内部程序。关于如何关闭安全模式,请访问网页http://www.php.net/manual/en/features.safe-mode.php,上面有详细的介绍。其次,在一些Unix系统上,PHP函数passthru不能将内部程序的输出传递给浏览页面,如果发生这种情况,可以用system函数来代替passthru函数。
四、结论
从本的例子可以看出,Unix操作系统非常强大,并且PHP允许开发者通过脚本以独立的线程来执行系统内部程序。本文的所给的例子非常的简单,但是只要再多花一点功夫,你可以写一个能更新Mysql数据库的c++程序,运行其他系统命令的程序或者是操作系统文件/目录结构的程序。但是,不管怎样,你都应该确保你的系统安全,绝对不能让任何其他的脚本程序随意访问系统内部程序。

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

PHP is widely used in e-commerce, content management systems and API development. 1) E-commerce: used for shopping cart function and payment processing. 2) Content management system: used for dynamic content generation and user management. 3) API development: used for RESTful API development and API security. Through performance optimization and best practices, the efficiency and maintainability of PHP applications are improved.

PHP and Python each have their own advantages, and the choice should be based on project requirements. 1.PHP is suitable for web development, with simple syntax and high execution efficiency. 2. Python is suitable for data science and machine learning, with concise syntax and rich libraries.

PHP is still dynamic and still occupies an important position in the field of modern programming. 1) PHP's simplicity and powerful community support make it widely used in web development; 2) Its flexibility and stability make it outstanding in handling web forms, database operations and file processing; 3) PHP is constantly evolving and optimizing, suitable for beginners and experienced developers.

PHP and Python have their own advantages and disadvantages, and the choice depends on project needs and personal preferences. 1.PHP is suitable for rapid development and maintenance of large-scale web applications. 2. Python dominates the field of data science and machine learning.

PHP is suitable for web development, especially in rapid development and processing dynamic content, but is not good at data science and enterprise-level applications. Compared with Python, PHP has more advantages in web development, but is not as good as Python in the field of data science; compared with Java, PHP performs worse in enterprise-level applications, but is more flexible in web development; compared with JavaScript, PHP is more concise in back-end development, but is not as good as JavaScript in front-end development.

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.
