CGI編程入門--GET與POST樣本
關於CGI的編程,我也還是新手!但只要懂C,則基於C的CGI編程就不會很難!
下面就GET和POST方法的應用,做一個小小的demo,給剛學習CGI編程的新手提供一點感性認識!
GET方法:做一個加法運算,需要接收兩個參數
檔案get.c如下:
-------------------------------
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
char *data;
char a[10],b[10];
printf("Content-Type:text/html/n/n");
printf("<HTML>/n");
printf("<HEAD>/n<TITLE >Get Method</TITLE>/n</HEAD>/n");
printf("<BODY>/n");
printf("<div style=/"font-size:12px/">/n");
data = getenv("QUERY_STRING");
if(sscanf(data,"a=%[^&]&b=%s",a,b)!=2){
printf("<DIV STYLE=/"COLOR:RED/">Error parameters should be entered!</DIV>/n");
}
else{
printf("<DIV STYLE=/"COLOR:GREEN;
font-size:15px;font-weight:bold/">a + b =
%d</DIV>/n",atoi(a)+atoi(b));
}
printf("<HR COLOR=/"blue/" align=/"left/" width=/"100/">");
printf("<input type=/"button/" value=/"Back CGI/" onclick=/"javascript:window.location='../cgi.html'/">");
printf("</div>/n");
printf("</BODY>/n");
printf("</HTML>/n");
return 0;
}
POST方法:做一個乘法運算,需要接收兩個參數
檔案post.c如下:
--------------------------------
#include <stdio.h>
#include <stdlib.h>
int main(void){
int len;
char *lenstr,poststr[20];
char m[10],n[10];
printf("Content-Type:text/html/n/n");
printf("<HTML>/n");
printf("<HEAD>/n<TITLE >post Method</TITLE>/n</HEAD>/n");
printf("<BODY>/n");
printf("<div style=/"font-size:12px/">/n");
lenstr=getenv("CONTENT_LENGTH");
if(lenstr == NULL)
printf("<DIV STYLE=/"COLOR:RED/">Error parameters should be entered!</DIV>/n");
else{
len=atoi(lenstr);
fgets(poststr,len+1,stdin);
if(sscanf(poststr,"m=%[^&]&n=%s",m,n)!=2){
printf("<DIV STYLE=/"COLOR:RED/">Error: Parameters are not right!</DIV>/n");
}
else{
printf("<DIV STYLE=/"COLOR:GREEN;
font-size:15px;font-weight:bold/">m * n =
%d</DIV>/n",atoi(m)*atoi(n));
}
}
printf("<HR COLOR=/"blue/" align=/"left/" width=/"100/">");
printf("<input type=/"button/" value=/"Back CGI/" onclick=/"javascript:window.location='../cgi.html'/">");
printf("</div>/n");
printf("</BODY>/n");
printf("</HTML>/n");
fflush(stdout);
return 0;
}
再附上html測試檔案cgi.html:
--------------------------------
<html>
<head>
<title>CGI Testing</title>
</head>
<body>
<table width="200" height="180" border="0" style="font-size:12px">
<tr><td>
<div style="font-weight:bold; font-size:15px">Method: GET</div>
<div>please input two number:<div>
<form method="get" action="./cgi-bin/get">
<input type="txt" size="3" name="a">+
<input type="txt" size="3" name="b">=
<input type="submit" value="sum">
</form>
</td></tr>
<tr><td>
<div style="font-weight:bold; font-size:15px">Method: POST</div>
<div>please input two number:<div>
<form method="post" action="./cgi-bin/post">
<input type="txt" size="3" name="m">*
<input type="txt" size="3" name="n">=
<input type="submit" value="resu">
</form>
</td></tr>
<tr><td><inputtype="button" value="Back
Home"onclick='javascript:window.location="./index.html"'></td></tr>
</table>
</body>
</html>
幾點簡要說明:
(1) printf("Content-Type:text/html/n/n");
此行通過標準輸出將字串″Contenttype:text/plain/n/n″傳送給Web伺服器。它是一個MIME頭資訊,它告訴Web伺服器隨
後的輸出是以純ASCII文本的形式。請注意在這個頭資訊中有兩個分行符號,這是因為Web伺服器需要在實際的文本資訊開始之前先看見一個空行。
(2) data = getenv("QUERY_STRING");
CGI定義:當GET方法提交的表單被發送到伺服器斷後,表單中的資料被儲存在伺服器上一個叫做QUERY_STRING的環境變數中。這種表單的處理相對簡單,只要讀取環境變數就可以了。
(3) sscanf(data,"a=%[^&]&b=%s",a,b)!=2
這個是關於sscanf函數的使用問題,自己可以上網搜尋一下,這裡不再詳述!
(4)atoi(a)+atoi(b)
atoi函數的功能是將字元型成整型,只有轉換之後才可以進行加法運算!
(5) lenstr=getenv("CONTENT_LENGTH");
Web伺服器在調用使用POST方法的CGI程式時設定此環境變數,它的文本值表示Web伺服器傳送給CGI程式的輸入中的字元數目,因此需要使用函數atoi() 將此環境變數的值轉換成整數,並賦給變數len(下面有定義)。
(6) fgets(poststr,len+1,stdin);
這個是關於fgets函數的使用問題,自己可以上網搜尋一下,這裡不再詳述!