如何使用C ++ CGI脚本?

时间:2011-01-28 02:31:52

标签: c++ html cgi

我目前正在我的大学注册一个Web应用程序课程,我们正在学习cgi脚本。我很难学习如何实现我的CGI脚本。当我点击我的链接时会弹出一个窗口,要求我下载helloworld.cgi文件而不是重定向。

HTML:

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

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;
    }

CGI脚本存储在/user/local/apache2/cgi-bin/helloworld.cgi

4 个答案:

答案 0 :(得分:13)

您需要编译C ++文件,并调用结果helloworld.cgi。 C ++不是脚本语言 - 您不能将其部署到服务器上。

在典型的* nix系统上,将C ++文件命名为helloworld.cpp

 gcc -o helloworld.cgi helloworld.cpp

然后将该文件放入cgi-bin

编辑:在最后一个标题项

之后需要两个endl
  cout << "Content-type: text/html" << endl << endl;

答案 1 :(得分:7)

/user/local/apache2/cgi-bin/helloworld.cgi是硬盘上文件的物理路径。要通过Apache运行脚本,您需要指定相对于服务器文档根目录的路径,例如。 http://localhost/cgi-bin/helloworld.cgi

答案 2 :(得分:2)

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/

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

And finally change href link to:

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

答案 3 :(得分:1)

您只需将Apache配置为正确识别cgi-bin ...

请仔细阅读:http://httpd.apache.org/docs/1.3/howto/cgi.html

在Apache配置ScriptAlias中可能就是你想要的。

(我假设您已将二进制文件编译为helloworld.cgi)

相关问题