2011-01-28 82 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>