简体   繁体   English

如何从Perl或PHP(在Linux上使用Apache的CGI-BIN)调用c ++二进制文件?

[英]How to call c++ binary from Perl or PHP (CGI-BIN using Apache on Linux )?

I have a website cgi-bin program that is written in c++. 我有一个用c ++编写的网站cgi-bin程序。

Unfortunately the website provider for my friend's site only allows Perl or PHP cgi-bin scripts. 不幸的是,我朋友网站的网站提供商仅允许Perl或PHP cgi-bin脚本。

Is there an easy way to simply have a very small Perl or PHP wrapper that just calls the c++ compiled binary? 有没有一种简单的方法来简单地拥有一个很小的Perl或PHP包装器,该包装器仅调用c ++编译的二进制文件?

Would the c++ program still be able to read from stdin for POST commands or would it have to be read by the Perl or PHP and then forwarded. C ++程序仍然能够从stdin中读取POST命令,还是必须由Perl或PHP读取然后转发。

You can use Perl's backticks or "system" commands to run shell commands. 您可以使用Perl的反引号或“系统”命令来运行Shell命令。 Also, perl has a lot of "Inline" classes that allow you to write code in other languages to be called in perl, including one for C++ . 另外,perl有很多“内联”类,使您可以用其他语言编写要在perl中调用的代码,包括一个用于C ++的代码 If you can't find something that works, maybe you can make your own wrapper using that package. 如果找不到有用的东西,也许可以使用该软件包制作自己的包装器。

You can use PHP's system() function to execute a shell command, which you could use to launch another program. 您可以使用PHP的system()函数执行一个shell命令,该命令可以用来启动另一个程序。 The STDOUT of that program would then go to the same place as php's stdout (to the HTTP connection). 然后,该程序的STDOUT将与php的stdout放在同一位置(到HTTP连接)。 You might have to do some messing around to get stdin to read from get/post/etc. 您可能需要做一些弄乱才能使stdin从get / post / etc中读取。

But most likely the website provider has disabled the ability of you to execute programs in this way. 但是,很可能网站提供商已禁用您以这种方式执行程序的功能。 It can be a security risk, and if they specifically only allow php and perl, then they would probably specifically disable as many methods of running non-php/perl as they could. 这可能会带来安全风险,如果它们专门只允许使用php和perl,则它们可能会专门禁用尽可能多的运行非php / perl的方法。 So, short answer is you're probably out of luck. 因此,简短的答案是您可能不走运。

See my Awstats Wrapper blog post for some PHP code that solved my problem. 有关解决我问题的一些PHP代码,请参阅我的Awstats包装器博客文章。 My approach was to construct a command line that has the right environment variables set from the CGI query params and then do substitutions on the resulting output so that any links back to the CGI would go to my wrapper script instead. 我的方法是构造一个命令行,该命令行具有从CGI查询参数设置的正确环境变量,然后对结果输出进行替换,以便所有返回CGI的链接都将转至我的包装器脚本。 I also nuked the headers output from the CGI since PHP provides its own headers. 由于PHP提供了自己的标头,因此我也对CGI的标头输出进行了修改。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM