當前位置:編程學習大全網 - 源碼下載 - 用php寫壹個網頁程序,功能是判斷服務器上QQ程序運行了多長時間

用php寫壹個網頁程序,功能是判斷服務器上QQ程序運行了多長時間

CGIC介紹

怎樣寫CGIC應用程序

怎樣產生圖片在CGIC中?

CGI調試特征: 利用捕獲

cgic函數參考

cgic變量參考

cgic結果編碼參考

cgic快速索引

壹般的UNIX系統都支持ANSIC,增加相應的庫函數(和相應的h文件)就可以實現CGI。在此我向大家推薦壹個用於CGI編程的ANSIC庫:cgic。

cgic是用來生成基於CGI的WWW應用程序的C語言函數庫,它有以下功能:

*對數據進行語法分析

*接收以GET和PSOT兩種方式發送的數據

*把FORM中的不同域連接成連續的串

*為檢索FORM數據而提供字符串,整數,浮點以及單項和多項選擇功能

*為數字字段提供邊界檢測

*把CGI環境變量加載到非空的C串中

*為調試而捕捉CGI狀態

*提供相對安全的系統調用功能

用壹般ANSI C或C++編譯器就可以編譯cgic程序,不過與通常C程序不同的是,用cgic寫的源碼其主函數是cgiMain(),而不是通常的main()。cgic的函數庫會自動把cgiMain連接到相應的main()上去。

--------------------------------------------------------------------------------

寫CGIC程序

Note: 所有的cgic應用程序必須連接cgic.c.

用cgimain()替代main() 必須包含: #include"cgic.h."

基本結構cgictest.c:

int cgiMain() {

#if DEBUG

/* Load a saved CGI scenario if we're debugging */

cgiReadEnvironment("/path/to/capcgi.dat");

#endif

/* Important: we must indicate the type of document */

cgiHeaderContentType("text/html");

/* Now invoke other functions to handle each part of the form */

fprintf(cgiOut, "<HTML><HEAD>\n");

fprintf(cgiOut, "<TITLE>cgic test</TITLE></HEAD>\n"):

fprintf(cgiOut, "<BODY><H1>cgic test</H1>\n");

Name();

Address();

Hungry();

Temperature();

Frogs();

Color();

Flavors();

NonExButtons();

RadioButtons();

fprintf(cgiOut, "</BODY></HTML>\n");

/* This value will be the exit code of the program; 0

generally indicates success among Unix and DOS programs */

return 0;

}

capture

輸出標頭

cgiHeaderContentType()在輸出文擋之前簡要說明MIME內型,如 "text/html"。

cgiHeaderStatus()代替輸出錯誤代碼 cgiHeaderLocation()代替重新引導至其他頁面。在壹個獨立的應用程序中只能有壹個cgiHeader函數。

重點:在cgiHeader函數組中, cgiHeaderContentType(), 在任何向瀏覽器輸出之前被調用. 否則將出錯或瀏覽器不能識別。 cgiOut

接著, cgiMain() 調用不同的函數.當函數結束後,將返回0

處理輸入文本

void Name() {

char name[81];

cgiFormStringNoNewlines("name", name, 81);

fprintf(cgiOut, "Name: %s<BR>\n", name);

}

這個函數的功能就是取的並顯示由用戶輸入的name .

處理輸出

Important: cgiOut通常相當於stdout

cgiFormString 確保斷航

處理單壹Checkboxes輸入

這個Hungry() function確定用戶是否選擇"hungry"這個 checkbox:

void Hungry() {

if (cgiFormCheckboxSingle("hungry") == cgiFormSuccess) {

fprintf(cgiOut, "I'm Hungry!<BR>\n");

} else {

fprintf(cgiOut, "I'm Not Hungry!<BR>\n");

}

}

這個函數依靠 cgiFormCheckboxSingle() 確定單壹的checkbox 被選擇。 cgiFormCheckboxSingle() 接受checkbox名字的屬性值,如果存在就返回 cgiFormSuccess,否則返回cgiFormNotFound 如果是多項checkboxes,就用 cgiFormCheckboxMultiple()和cgiFormStringMultiple() 函數.

處理數字輸入

Temperature() 返回浮點書的值確保在特定的返回內。

void Temperature() {

double temperature;

cgiFormDoubleBounded("temperature", &temperature, 80.0, 120.0, 98.6);

fprintf(cgiOut, "My temperature is %f.<BR>\n", temperature);

}

依靠cgiFormDoubleBounded()得到數據.第壹個數據是返回數據中輸入域的名字。最後壹個值是用戶沒有提交時的默認值。

這個函數總是找回在特定返回內合適的值; cgiFormDoubleBounded返回的值被檢查確信用戶輸入的資料在規定範圍內, 而不是其他無效的數據。查看 cgiFormDoubleBounded() 更多的資料. 如果限度檢查不理想,可以用 cgiFormDouble() 替代.

在整數輸入,cgiFormInteger 和 cgiFormIntegerBounded 可以利用. 這些函數的功能類似.

處理單壹選擇輸入

<SELECT> HTML標簽被用於向用戶提供幾個選擇. Radio buttons 和checkboxes 椰油這樣的用途,大門、能夠選擇的數量很小時. Color()

char *colors[] = {

"Red",

"Green",

"Blue"

};

void Color() {

int colorChoice;

cgiFormSelectSingle("colors", colors, 3, &colorChoice, 0);

fprintf(cgiOut, "I am: %s<BR>\n", colors[colorChoice]);

}

這個函數確定用戶選擇了幾個選項從<SELECT> 在表但的列表. cgiFormSelectSingle()

cgiFormSelectSingle() 總是顯示合理的選項值.

radio button也可以用這個函數.另外還有 cgiFormRadio(), 也是壹樣的

處理多項選擇的輸入

NonExButtons()

char *votes[] = {

"A",

"B",

"C",

"D"

};

void NonExButtons() {

int voteChoices[4];

int i;

int result;

int invalid;

char **responses;

/* Method #1: check for valid votes. This is a good idea,

since votes for nonexistent candidates should probably

be discounted... */

fprintf(cgiOut, "Votes (method 1):<BR>\n");

result = cgiFormCheckboxMultiple("vote", votes, 4,

voteChoices, &invalid);

if (result == cgiFormNotFound) {

fprintf(cgiOut, "I hate them all!<p>\n");

} else {

fprintf(cgiOut, "My preferred candidates are:\n");

fprintf(cgiOut, "<ul>\n");

for (i=0; (i < 4); i++) {

if (voteChoices[i]) {

fprintf(cgiOut, "<li>%s\n", votes[i]);

}

}

fprintf(cgiOut, "</ul>\n");

}

參考cgiFormCheckboxMultiple(), cgiFormSelectMultiple().

cgiFormCheckboxMultiple() cgiFormCheckboxMultiple

NonExButtons() 函數在 cgictest.c:

/* Method #2: get all the names voted for and trust them.

This is good if the form will change more often

than the code and invented responses are not a danger

or can be checked in some other way. */

fprintf(cgiOut, "Votes (method 2):<BR>\n");

result = cgiFormStringMultiple("vote", &responses);

if (result == cgiFormNotFound) {

fprintf(cgiOut, "I hate them all!<p>\n");

} else {

int i = 0;

fprintf(cgiOut, "My preferred candidates are:\n");

fprintf(cgiOut, "<ul>\n");

while (responses[i]) {

fprintf(cgiOut, "<li>%s\n", responses[i]);

i++;

}

fprintf(cgiOut, "</ul>\n");

}

/* We must be sure to free the string array or a memory

leak will occur. Simply calling free() would free

the array but not the individual strings. The

function cgiStringArrayFree() does the job completely. */

cgiStringArrayFree(responses);

}

參考cgiFormStringMultiple()

cgiFormStringMultiple()

/* An array of strings; each C string is an array of characters */

char **responses;

cgiFormStringMultiple("vote", &responses);

檢查CGI環境變量

將用到的變量 這裏,

產生圖象

#include "cgic.h"

#include "gd.h"

char *colors[] = {

"red", "green", "blue"

};

#define colorsTotal 3

int cgiMain() {

int colorChosen;

gdImagePtr im;

int r, g, b;

/* Use gd to create an image */

im = gdImageCreate(64, 64);

r = gdImageColorAllocate(im, 255, 0, 0);

g = gdImageColorAllocate(im, 0, 255, 0);

b = gdImageColorAllocate(im, 0, 0, 255);

/* Now use cgic to find out what color the user requested */

cgiFormSelectSingle("color", 3, &colorChosen, 0);

/* Now fill with the desired color */

switch(colorChosen) {

case 0:

gdImageFill(im, 32, 32, r);

break;

case 1:

gdImageFill(im, 32, 32, g);

break;

case 2:

gdImageFill(im, 32, 32, b);

break;

}

/* Now output the image. Note the content type! */

cgiHeaderContentType("image/gif");

/* Send the image to cgiOut */

gdImageGif(im, cgiOut);

/* Free the gd image */

gdImageDestroy(im);

return 0;

}

為調試而捕捉CGI狀態

cgic函數參考

cgiFormResultType cgiFormString( char *name, char *result, int max)

用於從輸入域中copy字符串。他將域名max-1字節中的字符copy到緩沖區result。若域不存在,則copy壹個空串到result緩沖區。在此函數中所有的新行由換行符代表。

cgiFormResultType cgiFormStringNoNewlines( char *name, char *result, int max)

它與cgiFormString函數相似,只是所有的CR和LF都被去掉了。

cgiFormResultType cgiFormStringSpaceNeeded( char *name, int *length)

它返回指向name的字符串的長度,並將長度放入length中。

cgiFormResultType cgiFormStringMultiple( char *name, char ***ptrToStringArray)

若同壹名字有多個輸入域,或域中的字符串可以動態變化,那麽妳可以使用本函數。它把名為name的所有輸入域的值放在prtToStringArray中。

void cgiStringArrayFree(char **stringArray)

它釋放了分配給stringArray的內存。

cgiFormResultType cgiFormInteger( char *name, int *result, int defaultV)

從輸入域中取出整數放入result中。

cgiFormResultType cgiFormIntegerBounded( char *name, int *result, int min, int max, int defaultV)

若輸入域中的整數在界限內則取出並放入result中。

cgiFormResultType cgiFormDouble( char *name, double *result, double defaultV)

從輸入域中取出浮點數放入result中。

cgiFormResultType cgiFormDoubleBounded( char *name, double *result, double min, double max, double defaultV)

若輸入域中的浮點數在界限內則取出並放入result中。

cgiFormResultType cgiFormSelectSingle( char *name, char **choicesText, int choicesTotal, int *result, int defaultV)

取出復選框(跟在select語句之後的),把選擇的名字copy到choicesText,把選擇的個數copy到choicesTotal,把當前的選擇copy到result。

cgiFormResultType cgiFormSelectMultiple( char *name, char **choicesText, int choicesTotal, int *result, int *invalid)

與cgiFormSelectSingle類似,只指向整型數組的result代表了選擇的項。

cgiFormResultType cgiFormCheckboxSingle( char *name)

若復選框被選中,則函數返回cgiFormSuccess,否則返回cgiFormNotFound。

cgiFormResultType cgiFormCheckboxMultiple( char *name, char **valuesText, int valuesTotal, int *result, int *invalid)

與cgiFormCheckboxSingle類似,但它處理同壹名字有多個復選框的情況。name指向復選框的名字;valuesText指向包含有每個復選框中參數的壹個數組;valuesTotal指向復選框的總數;result是壹個整型數組,每個復選框選中的用1代表,沒選中的用0代表。

cgiFormResultType cgiFormRadio( char *name, char **valuesText, int valuesTotal, int *result, int defaultV)

與cgiFormCheckboxMultiple相似,只是這裏是單選按鈕而不是復選框。

void cgiHeaderLocation(char *redirectUrl)

重定向到redirectUrl指定的URL。

void cgiHeaderStatus(int status, char *statusMessage)

輸出狀態代碼status和消息statusMessage。

void cgiHeaderContentType(char *mimeType)

用於告知瀏覽器返回的是什麽類型的文檔。

cgiEnvironmentResultType cgiWriteEnvironment(char *filename)

本函數把當前CGI環境寫入filename文件中以便以後調試時使用

cgiEnvironmentResultType cgiReadEnvironment(char *filename)

本函數從filename文件中讀取CGI環境以便用來調試。

int cgiMain()

壹個程序必須要寫這個函數, 這是主程序開始之處。

cgic變量參考

This section provides a reference guide to the various global variables provided by cgic for the programmer to utilize. These variables should always be used in preference to stdin, stdout, and calls to getenv() in order to ensure compatibility with the cgic CGI debugging features.

大多數的變量相當於各種CGI變量,重要的是VGIC的變量不能為空.

char *cgiServerSoftware

服務器軟件名稱,或者壹個空的字符串 or to an empty string if unknown.

char *cgiServerName

返回服務器名稱或空

char *cgiGatewayInterface

網關接口 (通常是 CGI/1.1),或空

char *cgiServerProtocol

網絡協議(usually HTTP/1.0),或空

char *cgiServerPort

服務器端口(usually 80),或空

char *cgiRequestMethod

請求方式 (usually GET or POST),或空

char *cgiPathInfo

指出附加虛擬路徑

char *cgiPathTranslated

指出附加虛擬路徑並由服務器轉為本地路徑

char *cgiScriptName

調用程序的名字

char *cgiQueryString

包含 GET-method 請求或者 <ISINDEX> 標簽. 這個信息不需要解吸,除非用<ISINDEX>標簽通常由CGIC函數庫自動解析。

char *cgiRemoteHost

從瀏覽器返回客戶主機的名字

char *cgiRemoteAddr

從瀏覽器返回客戶的IP地址

char *cgiAuthType

返回用戶授權信息

char *cgiRemoteUser

鑒別用戶 cgiAuthType.

char *cgiRemoteIdent

返回用戶的名字(用戶通過用戶堅定協議)這個消息是不安全的,特別是Windows系統。

char *cgiContentType

返回MIME內型

char *cgiAccept

參考 cgiHeaderContentType() cgiUserAgent

char *cgiUserAgent

取的用戶瀏覽器信息

char *cgiReferrer

指向用戶訪問的URL.

int cgiContentLength

表單或查詢數據的字節被認為是標準的.

FILE *cgiOut

CGI輸出. cgiHeader函數,象cgiHeaderContentType, 首先被用於輸出mime頭; 用於 fprintf() 和fwrite(). cgiOut通常相當於stdout。

FILE *cgiIn

CGI輸入. 在決大部分時間妳都不會需要這個函數。

cgic結果編碼參考

在大量的按列中, cgic函數有計劃的產生合理的結果,甚至瀏覽器和用戶不合理時。無論如何, 有時候知道不合理的事情發生,尤其賦予壹個值或定義壹個範圍是壹個不充分的解決方案。下面的這些結果編碼有助更好了解。

cgiFormSuccess

提交信息成功

cgiFormTruncated

刪除部分字節.

cgiFormBadType

錯誤的輸入信息(沒有按要求)

cgiFormEmpty

提交信息為空.

cgiFormNotFound

提交信息沒有找到.

cgiFormConstrained

數字屬於某個特定的範圍,被迫低於或高於適當範圍。

cgiFormNoSuchChoice

單壹選擇提交的值是不被接受。通常說明表但和程序之間存在矛盾。

cgiEnvironmentIO

從CGI環境或獲取的文件讀或寫的企圖失敗,報出I/O的錯誤。

cgiEnvironmentMemory

從CGI環境或獲取的文件讀或寫的企圖失敗,報出out-of-memory的錯誤。

cgiEnvironmentSuccess

從CGI環境或獲取的文件讀或寫的企圖成功。

cgic快速索引

  • 上一篇:北京app定制公司有哪些?哪家質量好?
  • 下一篇:女鬼病毒的傳播方式是什麽?
  • copyright 2024編程學習大全網