在cgic中印出form中所有的data是我覺得是非常重要的功能之一,因為這可以幫助你debug,印出cgi到底收到哪些from,以及其內容(value)為何。
首先利用cgiFormEntries()抓取form中所有欄位名稱(field name),接著在利用cgiFormStringNoNewlines()抓取內容(value)。
#include <stdio.h>
#include <string.h>
#include "cgic.h"
int cgiMain(int argc, char *argv[])
{
int ret;
char **array, **arrayStep, value[64];
cgiHeaderContentType("text/html");
if (cgiFormEntries(&array) != cgiFormSuccess) {
return -1;
}
for (arrayStep = array; *arrayStep; arrayStep++) {
snprintf(value, sizeof(value), "%s=", *arrayStep);
if ((ret =
cgiFormStringNoNewlines(*arrayStep, value + strlen(value),
sizeof(value) - strlen(value)))
!= cgiFormSuccess) {
fprintf(cgiOut, "<p>%s(null). failed</p>\n", value);
} else {
fprintf(cgiOut, "<p>%s</p>\n", value);
}
}
return 0;
}

沒有留言:
張貼留言