繁体   English   中英

如何使用C ++ CGI脚本?

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

我目前正在我的大学注册一个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

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

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

 gcc -o helloworld.cgi helloworld.cpp

然后将该文件放在cgi-bin中

编辑:你需要在最后一个标题项后面有两个endl

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

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

我也有这个问题,这个解决方案对我有用:
首先在终端上运行此命令:

sudo a2enmod cgi
sudo service apache2 restart

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

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

最后将href链接更改为:

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

您只需要配置Apache以正确识别cgi-bin ...

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

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

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

暂无
暂无

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

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