2011-01-28 80 views
2

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

+0

只是为了确认,运行此CGI在Web服务器上,而不是关闭本地文件? – 2011-01-28 02:40:21

+1

`/user`‽‽‽‽... – Hello71 2011-01-28 02:42:03

回答

4

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

9

您需要编译的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

我有这个问题太,而这个解决方案为我工作:
首先运行在终端这一命令:

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>