简体   繁体   English

如何使用C ++ CGI脚本?

[英]How To Use C++ CGI Script?

I am currently enrolled in a web applications class at my college and we are learning about cgi scripts. 我目前正在我的大学注册一个Web应用程序课程,我们正在学习cgi脚本。 I am having a hard time learning how to implement my CGI script. 我很难学习如何实现我的CGI脚本。 When I click on my link a window pops up asking me to download my helloworld.cgi file instead of just redirecting. 当我点击我的链接时会弹出一个窗口,要求我下载helloworld.cgi文件而不是重定向。

HTML: HTML:

<html>
    <body>
        <a href="/user/local/apache2/cgi-bin/helloworld.cgi">click me</a>
    </body>
</html>

C++: C ++:

#include <iostream>

using namespace std;

int main(){
    cout << "Content-type: text/html" << endl;
    cout << "<html>" << endl;
    cout << "   <body>" << endl;
    cout << "       Hello World!" << endl;
    cout << "   </body>" << endl;
    cout << "</html>" << endl;

    return 0;
    }

The CGI script is stored at /user/local/apache2/cgi-bin/helloworld.cgi CGI脚本存储在/user/local/apache2/cgi-bin/helloworld.cgi

You need to compile the C++ file, and call the result helloworld.cgi. 您需要编译C ++文件,并调用结果helloworld.cgi。 C++ is not a scripting language -- you can't just deploy it to your server. C ++不是脚本语言 - 您不能只将它部署到您的服务器上。

On a typical *nix system, name the C++ file helloworld.cpp 在典型的* nix系统上,将C ++文件命名为helloworld.cpp

 gcc -o helloworld.cgi helloworld.cpp

Then put that file in your cgi-bin 然后将该文件放在cgi-bin中

Edit: you need two endl's after the last header item 编辑:你需要在最后一个标题项后面有两个endl

  cout << "Content-type: text/html" << endl << endl;

/user/local/apache2/cgi-bin/helloworld.cgi is the physical path of the file on your hard disk. /user/local/apache2/cgi-bin/helloworld.cgi是硬盘上文件的物理路径。 To run the script through Apache, you need to specify the path relative to your server's document root, for eg. 要通过Apache运行脚本,您需要指定相对于服务器文档根目录的路径,例如。 http://localhost/cgi-bin/helloworld.cgi . http://localhost/cgi-bin/helloworld.cgi

I had this problem too, and this solution worked for me : 我也有这个问题,这个解决方案对我有用:
First run this commands on terminal: 首先在终端上运行此命令:

sudo a2enmod cgi
sudo service apache2 restart

Then copy helloworld.cgi to /usr/lib/cgi-bin/ 然后将helloworld.cgi复制到/ usr / lib / cgi-bin /

sudo cp helloworld.cgi /usr/lib/cgi-bin/

And finally change href link to: 最后将href链接更改为:

  <a href="/cgi-bin/helloworld.cgi">click me</a>

You just need to configure Apache to recognise a cgi-bin properly... 您只需要配置Apache以正确识别cgi-bin ...

Have a read of this: http://httpd.apache.org/docs/1.3/howto/cgi.html 请阅读: http//httpd.apache.org/docs/1.3/howto/cgi.html

In Apache config ScriptAlias is probably what you want. 在Apache配置ScriptAlias可能是你想要的。

(I'm assuming you've compiled the binary to helloworld.cgi) (我假设你已经将二进制文件编译为helloworld.cgi)

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

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