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

时间:2021-08-01 07:08:49

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读取然后转发。

3 个解决方案

#1


4  

You can use Perl's backticks or "system" commands to run shell commands. 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++. If you can't find something that works, maybe you can make your own wrapper using that package.

您可以使用Perl的反引号或“系统”命令来运行shell命令。此外,perl有很多“内联”类,允许您编写其他语言的代码,以便在perl中调用,包括一个用于C ++的代码。如果你找不到有用的东西,也许你可以使用那个包制作自己的包装。

#2


2  

You can use PHP's system() function to execute a shell command, which you could use to launch another program. The STDOUT of that program would then go to the same place as php's stdout (to the HTTP connection). You might have to do some messing around to get stdin to read from get/post/etc.

您可以使用PHP的system()函数执行shell命令,您可以使用它来启动另一个程序。然后该程序的STDOUT将转到与php的stdout(到HTTP连接)相同的位置。你可能不得不做一些搞乱让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. So, short answer is you're probably out of luck.

但很可能网站提供商已经禁用了以这种方式执行程序的能力。这可能是一个安全风险,如果他们特别只允许php和perl,那么他们可能会特别禁用尽可能多的运行非php / perl的方法。所以,简短的回答是你可能运气不好。

#3


0  

See my Awstats Wrapper blog post for some PHP code that solved my problem. 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. I also nuked the headers output from the CGI since PHP provides its own headers.

有关解决我问题的PHP代码,请参阅我的Awstats Wrapper博客文章。我的方法是构造一个命令行,该命令行具有从CGI查询参数设置的正确环境变量,然后对结果输出进行替换,以便返回CGI的任何链接将转到我的包装器脚本。我也从CGI输出了头文件,因为PHP提供了自己的头文件。

#1


4  

You can use Perl's backticks or "system" commands to run shell commands. 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++. If you can't find something that works, maybe you can make your own wrapper using that package.

您可以使用Perl的反引号或“系统”命令来运行shell命令。此外,perl有很多“内联”类,允许您编写其他语言的代码,以便在perl中调用,包括一个用于C ++的代码。如果你找不到有用的东西,也许你可以使用那个包制作自己的包装。

#2


2  

You can use PHP's system() function to execute a shell command, which you could use to launch another program. The STDOUT of that program would then go to the same place as php's stdout (to the HTTP connection). You might have to do some messing around to get stdin to read from get/post/etc.

您可以使用PHP的system()函数执行shell命令,您可以使用它来启动另一个程序。然后该程序的STDOUT将转到与php的stdout(到HTTP连接)相同的位置。你可能不得不做一些搞乱让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. So, short answer is you're probably out of luck.

但很可能网站提供商已经禁用了以这种方式执行程序的能力。这可能是一个安全风险,如果他们特别只允许php和perl,那么他们可能会特别禁用尽可能多的运行非php / perl的方法。所以,简短的回答是你可能运气不好。

#3


0  

See my Awstats Wrapper blog post for some PHP code that solved my problem. 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. I also nuked the headers output from the CGI since PHP provides its own headers.

有关解决我问题的PHP代码,请参阅我的Awstats Wrapper博客文章。我的方法是构造一个命令行,该命令行具有从CGI查询参数设置的正确环境变量,然后对结果输出进行替换,以便返回CGI的任何链接将转到我的包装器脚本。我也从CGI输出了头文件,因为PHP提供了自己的头文件。