c++ - 如何使用 C++ CGI 脚本?

标签 c++ html cgi

我目前正在我的大学注册网络应用程序类(class),我们正在学习 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;

关于c++ - 如何使用 C++ CGI 脚本?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4824273/

相关文章:

C++ GET 方法未处理的异常错误执行数学运算

c++ - 是否可以直接保留并复制到 std::string 中?

c++ - 数组的部分模板成员特化

html - 如何让 div 覆盖网页的整个宽度,而不仅仅是视口(viewport)?

html - 谁在复制/粘贴时进行转换?

perl - LWP 无法在 CGI 脚本中运行

python - 如何在tomcat服务器上运行python文件?

c++ - Linux 中的语音到文本转换

c++ - 创建一个可以是多种类型的 C++ 数组

html - Bootstrap 4 - 右对齐导航栏元素