轉自:http://blog.sina.com.cn/s/blog_60c00c780100rxxv.htmlhtml
1.CGI程序編寫 web
CGI是Common Gateway Interface的縮寫,翻譯成中文就是通用網關接口,它是網頁的後臺處理程序,運行在服務器端上,能夠用多種語言書寫,最經常使用的就是Perl(由於 Perl有強大的字符串處理功能,而CGI程序常常要處理許多的字符串)。舉個例子來講,一般通常的論壇或郵箱等都須要註冊,須要用戶輸入用戶名和密碼, 首先給你一個靜態的頁面,上面有兩個文本框,要求你輸入用戶名和密碼,還有一個提交和重置的按鈕,用於提交用戶的輸入,當用戶點擊提交按鈕時,這個請求就 被髮送到服務器端,服務器上的CGI程序就會解析用戶的輸入,而且驗證用戶的輸入是否合法,是否被經過驗證等。下面看一個很是簡單的例子,代碼是最好的解 說:apache
<html>
<head><title>用戶登錄驗證</title></head>
<body>
<form name="form1" action="/cgi-bin/output.cgi" method="POST">
<table align="center">
<tr><td align="center" colspan="2"></td></tr>
<tr>
<td align="right">用戶名</td>
<td><input type="text" name="Username"></td>
</tr>
<tr>
<td align="right">密 碼</td>
<td><input type="password" name="Password"></td>
</tr>
<tr>
<td><input type="submit" value="登 錄"></td>
<td><input type="reset" value="取 消"></td>
</tr>
</table>
</form>
</body>
</html>
-------------------------如下是cgi處理程序的源碼----------------------------
瀏覽器
#include <stdio.h>
#include <stdlib.h>
#include <string.h> 服務器
char* getcgidata(FILE* fp, char* requestmethod);網站
int main()spa
{翻譯
char *input;orm
char *req_method;server
char name[64];
char pass[64];
int i = 0;
int j = 0;
// printf("Content-type: text/plain; charset=iso-8859-1\n\n");
printf("Content-type: text/html\n\n");
printf("The following is query reuslt:<br><br>");
req_method = getenv("REQUEST_METHOD");
input = getcgidata(stdin, req_method);
// 咱們獲取的input字符串可能像以下的形式
// Username="admin"&Password="aaaaa"
// 其中"Username="和"&Password="都是固定的
// 而"admin"和"aaaaa"都是變化的,也是咱們要獲取的
// 前面9個字符是UserName=
// 在"UserName="和"&"之間的是咱們要取出來的用戶名
for ( i = 9; i < (int)strlen(input); i++ )
{
if ( input[i] == '&' )
{
name[j] = '\0';
break;
}
name[j++] = input[i];
}
// 前面9個字符 + "&Password="10個字符 + Username的字符數
// 是咱們不要的,故省略掉,不拷貝
for ( i = 19 + strlen(name), j = 0; i < (int)strlen(input); i++ )
{
pass[j++] = input[i];
}
pass[j] = '\0';
printf("Your Username is %s<br>Your Password is %s<br> \n", name, pass);
return 0;
}
char* getcgidata(FILE* fp, char* requestmethod)
{
char* input;
int len;
int size = 1024;
int i = 0;
if (!strcmp(requestmethod, "GET"))
{
input = getenv("QUERY_STRING");
return input;
}
else if (!strcmp(requestmethod, "POST"))
{
len = atoi(getenv("CONTENT_LENGTH"));
input = (char*)malloc(sizeof(char)*(size + 1));
if (len == 0)
{
input[0] = '\0';
return input;
}
while(1)
{
input[i] = (char)fgetc(fp);
if (i == size)
{
input[i+1] = '\0';
return input;
}
--len;
if (feof(fp) || (!(len)))
{
i++;
input[i] = '\0';
return input;
}
i++;
}
}
return NULL;
}
上面的第一個是一個靜態html頁面,要求用戶輸入用戶名和密碼,當戶輸入以後,點擊「登陸」按鈕,則用戶的輸入就被提交到服務器,由output.cgi來處理,在這裏,只是做爲一個演示,output.cgi把用戶的輸入顯示在頁面上。
操做提示:將第一段HTML代碼拷貝到一個文本文件,另存爲login.htm,注意擴展名要用htm或者html,第二段代碼是c語言源代碼,筆者在 VC++6.0下編譯經過,生成文件爲output.exe,將其更名爲output.cgi,login.htm放在網站的目錄,output.cgi 放在網站的cgi-bin目錄。筆者用的是Apache服務器,將login.htm放在了htdocs下面,將output.cgi放在了cgi- bin目錄。在瀏覽器輸入
http://127.0.0.1/login.htm
就會出現
這樣頁面,當用戶輸入了用戶名和密碼以後,點擊「登陸」按鈕,就會出現下面的頁面
The following is query reuslt:
Your Username is admin
Your Password is admin888
這裏咱們假設在用戶名文本框裏輸入的是"admin",在密碼框裏輸入的是"admin888"。
需 要說明是要在本機上運行該cgi程序,須要裝支持cgi的Web服務器,最多見的免費Web服務器就是apache,這個很容易下載到。安裝以後,基本不 須要作任何的配置,把login.htm放在htdocs目錄下面,把output.cgi放在cgi-bin目錄下,啓動web服務器後,就能夠解釋 cgi程序了。