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, "\n"); fprintf(cgiOut, "cgic test\n"): fprintf(cgiOut, "
cgic test
\n"); Name(); Address(); Hungry(); Temperature(); Frogs(); Color(); Flavors(); NonExButtons(); RadioButtons(); fprintf(cgiOut, "
\n"); /* This value will be the exit code of the program; 0 generally indicates success among Unix and DOS programs */ return 0; }
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): \n"); result = cgiFormCheckboxMultiple("vote", votes, 4, voteChoices, &invalid); if (result == cgiFormNotFound) { fprintf(cgiOut, "I hate them all!
/* 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): \n"); result = cgiFormStringMultiple("vote", &responses); if (result == cgiFormNotFound) { fprintf(cgiOut, "I hate them all!
\n"); } else { int i = 0; fprintf(cgiOut, "My preferred candidates are:\n"); fprintf(cgiOut, "
\n"); while (responses[i]) { fprintf(cgiOut, "
%s\n", responses[i]); i++; } fprintf(cgiOut, "
\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; }
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 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相似,只是这里是单选按钮而不是复选框。
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的变量不能为空.